開発環境
- macOS High Sierra - Apple
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
入門 Python 3 (Bill Lubanovic (著)、斎藤 康毅 (監修)、長尾 高弘 (翻訳)、オライリージャパン)の11章(並行処理とネットワーク)、11.3(復習問題)5.を取り組んでみる。
コード(Emacs)
Python 3
sub.py
#!/usr/bin/env python3
import zmq
import re
f1 = open('voewl.txt', 'w')
f2 = open('five.txt', 'w')
ctx = zmq.Context()
host = '127.0.0.1'
port = 6789
sub = ctx.socket(zmq.SUB)
sub.connect(f'tcp://{host}:{port}')
sub.setsockopt(zmq.SUBSCRIBE, b'')
while True:
word = sub.recv_string()
if word == 'END':
break
if re.match(r'[aeiou]', word):
print(word, file=f1)
if len(word) == 5:
print(word, file=f2)
f1.close()
f2.close()
pub.py
#!/usr/bin/env python3
import zmq
import time
filename = 'mammoth.txt'
with open(filename) as f:
mammoth = f.read()
f = open('pub.txt', 'w')
print('11-5', file=f)
host = '*'
port = 6789
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
pub.bind(f'tcp://{host}:{port}')
time.sleep(1)
for word in [w.strip() for w in mammoth.split()]:
print(word, file=f)
pub.send_string(word)
f.close()
入出力結果(Terminal, Jupyter(IPython))
$ ./sub.py & [1] 79144 $ ./pub.py [1]+ Done ./sub.py $ cat voewl.txt of at ease, evening admired a of as a of as upon unrivalled, of a as off as as at of of or oh! of a even at it and $ cat five.txt thee, queen Lying ease, flies gaily great show, swarm bees, stand queen heard great youth might songs glees could sing, queen We'rt You'd shade noon, Folks would think About crush soon. $
0 コメント:
コメントを投稿