開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Introducing Python(Bill Lubanovic (著)、O'Reilly Media)のChapter 11(Concurrency and Networks)、Things to Do 11.4.を解いてみる。
Things to Do 11.4.
コード
server
#!/usr/bin/env python3 #-*- coding: utf-8 -*- import redis import time import random conn = redis.Redis() chocolates = ['type1', 'type2', 'type3', 'type4', 'type5'] for _ in range(100): conn.rpush('chocolates', random.choice(chocolates).encode('utf-8')) t = random.random() print('sleep: {0}'.format(t)) time.sleep(t) conn.rpush('chocolates', 'quit')
client
#!/usr/bin/env python3 #-*- coding: utf-8 -*- import redis import datetime import time conn = redis.Redis() while True: msg = conn.blpop('chocolates', timeout=60) if msg: val = msg[1].decode('utf-8') if val == 'quit': break print('{0}: {1}, 残り: {2}個'.format( datetime.datetime.utcnow().time().isoformat(), val, conn.llen('chocolates'))) time.sleep(0.5)
入出力結果(Terminal, IPython)
redis-server
$ redis-server 98953:C 22 Jan 16:17:28.676 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf 98953:M 22 Jan 16:17:28.680 * Increased maximum number of open files to 10032 (it was originally set to 256). _._ _.-``__ ''-._ _.-`` `. `_. ''-._ Redis 3.2.6 (00000000/0) 64 bit .-`` .-```. ```\/ _.,_ ''-._ ( ' , .-` | `, ) Running in standalone mode |`-._`-...-` __...-.``-._|'` _.-'| Port: 6379 | `-._ `._ / _.-' | PID: 98953 `-._ `-._ `-./ _.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | http://redis.io `-._ `-._`-.__.-'_.-' _.-' |`-._`-._ `-.__.-' _.-'_.-'| | `-._`-._ _.-'_.-' | `-._ `-._`-.__.-'_.-' _.-' `-._ `-.__.-' _.-' `-._ _.-' `-.__.-' 98953:M 22 Jan 16:17:28.684 # Server started, Redis version 3.2.6 98953:M 22 Jan 16:17:28.685 * DB loaded from disk: 0.000 seconds 98953:M 22 Jan 16:17:28.685 * The server is now ready to accept connections on port 6379 ^C98953:signal-handler (1485069561) Received SIGINT scheduling shutdown... 98953:M 22 Jan 16:19:21.332 # User requested shutdown... 98953:M 22 Jan 16:19:21.332 * Saving the final RDB snapshot before exiting. 98953:M 22 Jan 16:19:21.337 * DB saved on disk 98953:M 22 Jan 16:19:21.337 # Redis is now ready to exit, bye bye... $
server
$ ./sample4_client.py 07:17:38.758256: type4, 残り: 0個 07:17:39.262726: type5, 残り: 1個 07:17:39.768716: type5, 残り: 0個 07:17:40.271175: type3, 残り: 0個 07:17:40.776591: type3, 残り: 1個 07:17:41.279685: type5, 残り: 0個 07:17:41.782624: type2, 残り: 0個 07:17:42.285622: type5, 残り: 0個 07:17:42.788697: type5, 残り: 1個 07:17:43.291918: type5, 残り: 0個 07:17:43.795143: type1, 残り: 1個 07:17:44.297972: type5, 残り: 2個 07:17:44.800833: type1, 残り: 2個 07:17:45.303845: type3, 残り: 2個 07:17:45.806335: type1, 残り: 1個 07:17:46.309432: type5, 残り: 1個 07:17:46.812556: type3, 残り: 0個 07:17:47.358980: type3, 残り: 0個 07:17:47.864323: type4, 残り: 1個 07:17:48.384154: type5, 残り: 0個 07:17:48.886392: type1, 残り: 0個 07:17:49.630549: type4, 残り: 0個 07:17:50.133121: type4, 残り: 1個 07:17:50.636402: type5, 残り: 0個 07:17:51.139643: type5, 残り: 0個 07:17:51.642557: type2, 残り: 2個 07:17:52.145497: type4, 残り: 2個 07:17:52.648508: type4, 残り: 2個 07:17:53.151393: type2, 残り: 3個 07:17:53.654343: type3, 残り: 2個 07:17:54.156582: type4, 残り: 3個 07:17:54.662318: type2, 残り: 4個 07:17:55.168572: type3, 残り: 4個 07:17:55.673327: type2, 残り: 3個 07:17:56.176195: type1, 残り: 3個 07:17:56.679200: type4, 残り: 3個 07:17:57.181675: type2, 残り: 3個 07:17:57.684869: type3, 残り: 3個 07:17:58.191111: type2, 残り: 2個 07:17:58.694073: type5, 残り: 3個 07:17:59.196564: type2, 残り: 3個 07:17:59.703060: type1, 残り: 3個 07:18:00.207494: type3, 残り: 2個 07:18:00.710692: type5, 残り: 2個 07:18:01.213161: type4, 残り: 2個 07:18:01.716171: type1, 残り: 4個 07:18:02.220758: type3, 残り: 4個 07:18:02.723865: type3, 残り: 3個 07:18:03.226973: type1, 残り: 3個 07:18:03.729956: type3, 残り: 4個 07:18:04.232868: type1, 残り: 3個 07:18:04.738678: type4, 残り: 4個 07:18:05.242360: type5, 残り: 6個 07:18:05.744679: type4, 残り: 6個 07:18:06.246863: type4, 残り: 6個 07:18:06.749820: type4, 残り: 6個 07:18:07.254800: type3, 残り: 5個 07:18:07.757700: type2, 残り: 7個 07:18:08.260635: type2, 残り: 6個 07:18:08.763553: type2, 残り: 7個 07:18:09.266371: type3, 残り: 7個 07:18:09.768944: type2, 残り: 6個 07:18:10.271824: type1, 残り: 7個 07:18:10.774934: type1, 残り: 7個 07:18:11.278000: type5, 残り: 6個 07:18:11.779608: type4, 残り: 7個 07:18:12.282425: type3, 残り: 7個 07:18:12.785438: type4, 残り: 8個 07:18:13.291968: type3, 残り: 8個 07:18:13.795213: type5, 残り: 8個 07:18:14.300006: type4, 残り: 7個 07:18:14.805494: type3, 残り: 7個 07:18:15.310626: type1, 残り: 6個 07:18:15.813119: type2, 残り: 6個 07:18:16.317891: type1, 残り: 6個 07:18:16.820825: type1, 残り: 5個 07:18:17.323796: type3, 残り: 5個 07:18:17.826699: type2, 残り: 5個 07:18:18.333555: type3, 残り: 4個 07:18:18.840464: type1, 残り: 5個 07:18:19.343749: type2, 残り: 4個 07:18:19.846578: type2, 残り: 6個 07:18:20.348538: type4, 残り: 7個 07:18:20.851316: type3, 残り: 6個 07:18:21.354617: type1, 残り: 6個 07:18:21.856699: type3, 残り: 6個 07:18:22.359648: type2, 残り: 6個 07:18:22.864919: type4, 残り: 5個 07:18:23.367297: type4, 残り: 5個 07:18:23.870076: type5, 残り: 4個 07:18:24.373162: type4, 残り: 5個 07:18:24.876126: type4, 残り: 4個 07:18:25.379075: type4, 残り: 4個 07:18:25.883189: type2, 残り: 4個 07:18:26.389513: type3, 残り: 3個 07:18:26.894029: type1, 残り: 3個 07:18:27.398411: type1, 残り: 3個 07:18:27.901333: type1, 残り: 2個 07:18:28.403602: type5, 残り: 2個 07:18:28.907013: type3, 残り: 1個 $
client
$ ./sample4_server.py sleep: 0.4117991341707955 sleep: 0.04998141506077591 sleep: 0.7485025008299782 sleep: 0.3255470924249928 sleep: 0.159430719793908 sleep: 0.9834919299365433 sleep: 0.3469547762354729 sleep: 0.755312456352264 sleep: 0.03378718499915789 sleep: 0.91993021958701 sleep: 0.14170314246022941 sleep: 0.455184406230016 sleep: 0.128928802172658 sleep: 0.21591111474888114 sleep: 0.8082855269378304 sleep: 0.8672118777512746 sleep: 0.7325166665259504 sleep: 0.5557384245538682 sleep: 0.33055751876397077 sleep: 0.9379433331774739 sleep: 0.8852367814152841 sleep: 0.09848363568647744 sleep: 0.06428800695594328 sleep: 0.9296166461895936 sleep: 0.4066955448726376 sleep: 0.36047711784079617 sleep: 0.10942463055248108 sleep: 0.08056953837982683 sleep: 0.6781686067175158 sleep: 0.5841897091186365 sleep: 0.0012427273333498468 sleep: 0.6992792168144984 sleep: 0.3896268056058029 sleep: 0.10313616890248789 sleep: 0.45886309083011756 sleep: 0.3135398086519495 sleep: 0.7959214422928212 sleep: 0.6380567809220093 sleep: 0.43677389419432666 sleep: 0.7194594092685099 sleep: 0.6186588165155706 sleep: 0.3663222331839465 sleep: 0.531211486953143 sleep: 0.5900423082475513 sleep: 0.7437958501941301 sleep: 0.4173266016809781 sleep: 0.47744972813623043 sleep: 0.016654642834315614 sleep: 0.0796613631497991 sleep: 0.6092920582694924 sleep: 0.6663329452940869 sleep: 0.7453242226205685 sleep: 0.1511244182732555 sleep: 0.7623924702580542 sleep: 0.22982295321036028 sleep: 0.15348114389170397 sleep: 0.08112976632816216 sleep: 0.29111406265317397 sleep: 0.5358985599899889 sleep: 0.4218764346533883 sleep: 0.3283288793101137 sleep: 0.9513001203228056 sleep: 0.10434009191329896 sleep: 0.14800607790335907 sleep: 0.5959470550681147 sleep: 0.0603635177593802 sleep: 0.8298699879478828 sleep: 0.7123218130084553 sleep: 0.3051302278705327 sleep: 0.1764495562119308 sleep: 0.9224779675276471 sleep: 0.4330122605277956 sleep: 0.2248868208461493 sleep: 0.3938069092810006 sleep: 0.21149606544442434 sleep: 0.3660792626560655 sleep: 0.5315169564258753 sleep: 0.9518243334660906 sleep: 0.9971705076574947 sleep: 0.7615355829842757 sleep: 0.6905799363891454 sleep: 0.8621109514718538 sleep: 0.6577261364988285 sleep: 0.32703986088305703 sleep: 0.6902959906774928 sleep: 0.07990625588218991 sleep: 0.23980944399944581 sleep: 0.24429483580301847 sleep: 0.2286015003724563 sleep: 0.5654192614942197 sleep: 0.7224965926378939 sleep: 0.3619973035233034 sleep: 0.9741973673752164 sleep: 0.9123089330115831 sleep: 0.29310492225843776 sleep: 0.962025098266336 sleep: 0.5663352949218927 sleep: 0.7872895820393601 sleep: 0.8749132276564258 sleep: 0.5517753668817553 $
0 コメント:
コメントを投稿