開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
入門 Python 3 (Bill Lubanovic (著)、 斎藤 康毅(監修)、 長尾 高弘 (翻訳)、オライリージャパン)の11章(並行処理とネットワーク)、11.3(復習問題)、11-4を取り組んでみる。
11-4
コード(Emacs)
factory.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import redis
import time
import random
host = 'localhost'
port = 6379
conn = redis.Redis(host=host, port=port)
name = 'chocolates'
conn.delete(name)
chocolates = ['type{0}'.format(i + 1) for i in range(5)]
while True:
time.sleep(random.random())
conn.rpush('chocolates', random.choice(chocolates))
lucy.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import redis
import time
import datetime
host = 'localhost'
port = 6379
conn = redis.Redis(host=host, port=port)
name = 'chocolates'
while True:
t = conn.lpop(name)
print(datetime.datetime.utcnow())
print(t.decode('utf-8'))
l = conn.llen(name)
time.sleep(0.5)
print('残り: {0}'.format(l))
if l == 0:
print('終了!')
break
入出力結果(Terminal, IPython)
$ sudo port load redis $ ./factory.py & [1] 17569 $ ./lucy.py 2016-09-07 11:25:37.669114 type1 残り: 2 2016-09-07 11:25:38.171688 type2 残り: 2 2016-09-07 11:25:38.678008 type3 残り: 1 2016-09-07 11:25:39.182196 type4 残り: 1 2016-09-07 11:25:39.688896 type2 残り: 3 2016-09-07 11:25:40.195779 type5 残り: 3 2016-09-07 11:25:40.698051 type4 残り: 3 2016-09-07 11:25:41.202121 type1 残り: 2 2016-09-07 11:25:41.703932 type1 残り: 2 2016-09-07 11:25:42.210413 type4 残り: 2 2016-09-07 11:25:42.714215 type1 残り: 3 2016-09-07 11:25:43.216819 type3 残り: 3 2016-09-07 11:25:43.720093 type4 残り: 4 2016-09-07 11:25:44.224166 type2 残り: 4 2016-09-07 11:25:44.726231 type4 残り: 3 2016-09-07 11:25:45.232802 type3 残り: 3 2016-09-07 11:25:45.736205 type4 残り: 2 2016-09-07 11:25:46.238677 type5 残り: 2 2016-09-07 11:25:46.741533 type3 残り: 2 2016-09-07 11:25:47.249328 type4 残り: 2 2016-09-07 11:25:47.751569 type3 残り: 2 2016-09-07 11:25:48.254180 type1 残り: 2 2016-09-07 11:25:48.761652 type1 残り: 3 2016-09-07 11:25:49.265048 type5 残り: 5 2016-09-07 11:25:49.771343 type5 残り: 5 2016-09-07 11:25:50.275476 type4 残り: 5 2016-09-07 11:25:50.778335 type5 残り: 4 2016-09-07 11:25:51.281541 type5 残り: 4 2016-09-07 11:25:51.783742 type2 残り: 4 2016-09-07 11:25:52.289554 type2 残り: 3 2016-09-07 11:25:52.795872 type5 残り: 3 2016-09-07 11:25:53.302913 type5 残り: 3 2016-09-07 11:25:53.809406 type3 残り: 3 2016-09-07 11:25:54.316386 type3 残り: 3 2016-09-07 11:25:54.820813 type3 残り: 2 2016-09-07 11:25:55.327423 type5 残り: 2 2016-09-07 11:25:55.832352 type2 残り: 1 2016-09-07 11:25:56.335538 type5 残り: 2 2016-09-07 11:25:56.839973 type3 残り: 2 2016-09-07 11:25:57.342357 type2 残り: 2 2016-09-07 11:25:57.846712 type3 残り: 1 2016-09-07 11:25:58.353433 type5 残り: 2 2016-09-07 11:25:58.858911 type5 残り: 2 2016-09-07 11:25:59.361614 type2 残り: 1 2016-09-07 11:25:59.866014 type4 残り: 1 2016-09-07 11:26:00.368172 type1 残り: 1 2016-09-07 11:26:00.871562 type5 残り: 0 終了! $ fg ./factory.py C-c C-cTraceback (most recent call last): File "./factory.py", line 17, in <module> time.sleep(random.random()) KeyboardInterrupt $ sudo port unload redis $
0 コメント:
コメントを投稿