Last active
December 17, 2015 12:09
-
-
Save max747/5607339 to your computer and use it in GitHub Desktop.
pyzmq のクライアント側を multiprocessing でぶん回す実験
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import functools | |
import random | |
import sys | |
from multiprocessing import Pool | |
class Client(object): | |
def __init__(self, client_id, addr): | |
self.ctx = zmq.Context() | |
self.socket = self.ctx.socket(zmq.REQ) | |
self.socket.connect(addr) | |
self.client_id = client_id | |
def echo(self): | |
message = ''.join([chr(random.randrange(65, 90)) for i in xrange(8)]) | |
self.socket.send(self.client_id + ' ' + message) | |
reply = self.socket.recv() | |
def zmq_echo(client_id, repeat): | |
client = Client(str(client_id), "ipc:///tmp/zmq.sock") | |
[client.echo() for i in xrange(repeat)] | |
if __name__ == "__main__": | |
processes = int(sys.argv[1]) | |
repeat = 25200 / processes | |
p = Pool(processes) | |
_zmq_echo = functools.partial(zmq_echo, repeat=repeat) | |
p.map(_zmq_echo, xrange(processes)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% for i in $(seq 10); do | |
echo "=== processes: $i ===" | |
time python client.py $i | |
time python client.py $i | |
time python client.py $i | |
done | |
=== processes: 1 === | |
python client.py $i 2.00s user 1.57s system 46% cpu 7.762 total | |
python client.py $i 2.01s user 1.58s system 45% cpu 7.863 total | |
python client.py $i 2.01s user 1.59s system 45% cpu 7.860 total | |
=== processes: 2 === | |
python client.py $i 1.97s user 1.52s system 59% cpu 5.884 total | |
python client.py $i 1.95s user 1.53s system 58% cpu 5.910 total | |
python client.py $i 1.98s user 1.52s system 58% cpu 5.990 total | |
=== processes: 3 === | |
python client.py $i 1.96s user 1.52s system 68% cpu 5.073 total | |
python client.py $i 1.97s user 1.52s system 68% cpu 5.091 total | |
python client.py $i 1.97s user 1.53s system 70% cpu 4.968 total | |
=== processes: 4 === | |
python client.py $i 1.95s user 1.51s system 74% cpu 4.672 total | |
python client.py $i 1.94s user 1.51s system 72% cpu 4.777 total | |
python client.py $i 1.95s user 1.51s system 74% cpu 4.675 total | |
=== processes: 5 === | |
python client.py $i 1.95s user 1.51s system 77% cpu 4.486 total | |
python client.py $i 1.94s user 1.50s system 74% cpu 4.592 total | |
python client.py $i 1.94s user 1.51s system 78% cpu 4.371 total | |
=== processes: 6 === | |
python client.py $i 1.95s user 1.51s system 83% cpu 4.170 total | |
python client.py $i 1.94s user 1.51s system 82% cpu 4.168 total | |
python client.py $i 1.95s user 1.53s system 81% cpu 4.275 total | |
=== processes: 7 === | |
python client.py $i 1.96s user 1.53s system 83% cpu 4.174 total | |
python client.py $i 1.96s user 1.53s system 83% cpu 4.175 total | |
python client.py $i 1.96s user 1.54s system 85% cpu 4.076 total | |
=== processes: 8 === | |
python client.py $i 1.96s user 1.54s system 87% cpu 3.974 total | |
python client.py $i 1.96s user 1.53s system 87% cpu 3.975 total | |
python client.py $i 1.96s user 1.54s system 87% cpu 3.983 total | |
=== processes: 9 === | |
python client.py $i 1.96s user 1.54s system 89% cpu 3.886 total | |
python client.py $i 1.96s user 1.55s system 89% cpu 3.895 total | |
python client.py $i 1.95s user 1.53s system 89% cpu 3.877 total | |
=== processes: 10 === | |
python client.py $i 1.97s user 1.56s system 91% cpu 3.879 total | |
python client.py $i 1.97s user 1.55s system 90% cpu 3.877 total | |
python client.py $i 1.96s user 1.55s system 90% cpu 3.874 total |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import time | |
class Server(object): | |
def __init__(self, addr): | |
self.ctx = zmq.Context() | |
self.socket = self.ctx.socket(zmq.REP) | |
self.socket.bind(addr) | |
print "Server start..." | |
def run(self): | |
try: | |
while True: | |
message = self.socket.recv() | |
print time.time(), message | |
self.socket.send(message) | |
except KeyboardInterrupt: | |
pass | |
if __name__ == "__main__": | |
server = Server("ipc:///tmp/zmq.sock") | |
server.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment