2018年6月8日金曜日

開発環境

入門 Python 3 (Bill Lubanovic (著)、斎藤 康毅 (監修)、長尾 高弘 (翻訳)、オライリージャパン)の11章(並行処理とネットワーク)、11.3(復習問題)4.を取り組んでみる。

コード(Emacs)

Python 3

redis_push.py

#!/usr/bin/env python3
import redis
import random
import time

f = open('push.txt', 'w')
print('11-4', file=f)

chocolates = [f'chocolate{i}' for i in range(1, 11)]
conn = redis.Redis()
name = 'chocolates'
conn.delete(name)
for _ in range(50):
    chocolate = random.choice(chocolates)
    conn.rpush(name, chocolate.encode('utf-8'))
    seconds = random.random()
    print(f'{seconds:.2f}: {chocolate}', file=f)
    time.sleep(seconds)

redis_pop.py

#!/usr/bin/env python3
import redis
import time
import datetime

print('11-4')
conn = redis.Redis()
keys = 'chocolates'
i = 0
while True:
    chocolate = conn.blpop(keys)
    if chocolate:
        i += 1
        time.sleep(0.5)
        print(chocolate[1].decode('utf-8'))
        print(f'{datetime.datetime.now().isoformat()} 残り: {conn.llen(keys)}')
        if i == 50:
            break

入出力結果(Terminal, Jupyter(IPython))

$ ./redis_push.py & ./redis_pop.py
[1] 72925
11-4
chocolate4
2018-06-08T14:42:48.348158 残り: 1
chocolate10
2018-06-08T14:42:48.853751 残り: 1
chocolate10
2018-06-08T14:42:49.359299 残り: 1
chocolate3
2018-06-08T14:42:49.864873 残り: 0
chocolate7
2018-06-08T14:42:50.659937 残り: 0
chocolate4
2018-06-08T14:42:51.428785 残り: 0
chocolate9
2018-06-08T14:42:52.191490 残り: 0
chocolate10
2018-06-08T14:42:53.078798 残り: 0
chocolate6
2018-06-08T14:42:53.718800 残り: 0
chocolate2
2018-06-08T14:42:54.656345 残り: 0
chocolate1
2018-06-08T14:42:55.177991 残り: 0
chocolate3
2018-06-08T14:42:55.808047 残り: 2
chocolate5
2018-06-08T14:42:56.313629 残り: 2
chocolate5
2018-06-08T14:42:56.819238 残り: 2
chocolate2
2018-06-08T14:42:57.324822 残り: 3
chocolate8
2018-06-08T14:42:57.830381 残り: 3
chocolate8
2018-06-08T14:42:58.335965 残り: 3
chocolate6
2018-06-08T14:42:58.840620 残り: 3
chocolate3
2018-06-08T14:42:59.342933 残り: 2
chocolate4
2018-06-08T14:42:59.847394 残り: 2
chocolate4
2018-06-08T14:43:00.348406 残り: 2
chocolate9
2018-06-08T14:43:00.853980 残り: 2
chocolate2
2018-06-08T14:43:01.359521 残り: 3
chocolate10
2018-06-08T14:43:01.865080 残り: 2
chocolate9
2018-06-08T14:43:02.370642 残り: 3
chocolate6
2018-06-08T14:43:02.876201 残り: 5
chocolate3
2018-06-08T14:43:03.381775 残り: 4
chocolate9
2018-06-08T14:43:03.882801 残り: 5
chocolate4
2018-06-08T14:43:04.388325 残り: 5
chocolate3
2018-06-08T14:43:04.893919 残り: 5
chocolate10
2018-06-08T14:43:05.396244 残り: 5
chocolate4
2018-06-08T14:43:05.901794 残り: 5
chocolate1
2018-06-08T14:43:06.407322 残り: 5
chocolate3
2018-06-08T14:43:06.912869 残り: 5
chocolate10
2018-06-08T14:43:07.418439 残り: 5
chocolate1
2018-06-08T14:43:07.923977 残り: 4
chocolate9
2018-06-08T14:43:08.429537 残り: 4
chocolate1
2018-06-08T14:43:08.935102 残り: 3
chocolate1
2018-06-08T14:43:09.440651 残り: 4
chocolate8
2018-06-08T14:43:09.946181 残り: 3
chocolate9
2018-06-08T14:43:10.451741 残り: 3
chocolate2
2018-06-08T14:43:10.957258 残り: 3
chocolate2
2018-06-08T14:43:11.462826 残り: 3
chocolate6
2018-06-08T14:43:11.968366 残り: 4
chocolate2
2018-06-08T14:43:12.472730 残り: 3
chocolate2
2018-06-08T14:43:12.978291 残り: 4
chocolate5
2018-06-08T14:43:13.483886 残り: 3
chocolate4
2018-06-08T14:43:13.989433 残り: 2
chocolate2
2018-06-08T14:43:14.494999 残り: 1
chocolate7
2018-06-08T14:43:15.000573 残り: 0
[1]+  Done                    ./redis_push.py
$ cat push.txt
11-4
0.48: chocolate4
0.47: chocolate10
0.43: chocolate10
0.91: chocolate3
0.76: chocolate7
0.76: chocolate4
0.88: chocolate9
0.64: chocolate10
0.93: chocolate6
0.52: chocolate2
0.62: chocolate1
0.36: chocolate3
0.08: chocolate5
0.48: chocolate5
0.11: chocolate2
0.70: chocolate8
0.19: chocolate8
0.30: chocolate6
0.30: chocolate3
0.97: chocolate4
0.88: chocolate4
0.25: chocolate9
0.83: chocolate2
0.25: chocolate10
0.01: chocolate9
0.91: chocolate6
0.04: chocolate3
0.35: chocolate9
0.10: chocolate4
0.06: chocolate3
0.94: chocolate10
0.19: chocolate4
0.21: chocolate1
0.72: chocolate3
0.49: chocolate10
0.62: chocolate1
0.57: chocolate9
0.10: chocolate1
0.72: chocolate1
0.99: chocolate8
0.98: chocolate9
0.26: chocolate2
0.93: chocolate2
0.12: chocolate6
0.91: chocolate2
0.23: chocolate2
0.04: chocolate5
0.83: chocolate4
0.32: chocolate2
0.57: chocolate7
$ 

0 コメント:

コメントを投稿

関連コンテンツ