開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
Introducing Python: Modern Computing in Simple Packages(Bill Lubanovic (著)、 O'Reilly Media)のChapter 11(Concurrency and Networks)、Things to Do 11.4.を解いてみる。
Things to Do 11.4.
コード(Emacs, BBEdit)
server
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import redis
import time
import random
conn = redis.Redis()
chocolates = ['c1', 'c2', 'c3', 'c4', 'c5']
l = 'chocolates'
for chocolate in chocolates:
msg = chocolate.encode('utf-8')
conn.rpush(l, msg)
conn.rpush('chocolates', 'quit')
client
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import redis
import time
import datetime
conn = redis.Redis()
l = 'chocolates'
duration = 0.5
while True:
time.sleep(duration)
msg = conn.blpop(l)
if not msg:
break
val = msg[1].decode('utf-8')
if val == 'quit':
break
print(datetime.datetime.utcnow(), val, conn.llen(l))
入出力結果(Terminal, IPython)
$ redis-server & ./sample4_server.py & ./sample4_client.py
[1] 50278
[2] 50279
[50278] 10 May 15:37:05.806 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
[50278] 10 May 15:37:05.814 * Increased maximum number of open files to 10032 (it was originally set to 256).
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 2.8.17 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in stand alone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 50278
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
[50278] 10 May 15:37:05.842 # Server started, Redis version 2.8.17
[50278] 10 May 15:37:05.842 * DB loaded from disk: 0.000 seconds
[50278] 10 May 15:37:05.842 * The server is now ready to accept connections on port 6379
2015-05-10 06:37:06.704135 c1 5
2015-05-10 06:37:07.209497 c2 4
2015-05-10 06:37:07.715719 c3 3
2015-05-10 06:37:08.220876 c4 2
2015-05-10 06:37:08.725962 c5 1
[2]+ Done ./sample4_server.py
$ fg
redis-server
C-c C-c[50278 | signal handler] (1431239840) Received SIGINT scheduling shutdown...
[50278] 10 May 15:37:21.051 # User requested shutdown...
[50278] 10 May 15:37:21.051 * Saving the final RDB snapshot before exiting.
[50278] 10 May 15:37:21.055 * DB saved on disk
[50278] 10 May 15:37:21.055 # Redis is now ready to exit, bye bye...
$
0 コメント:
コメントを投稿
Comments on Google+: