Created
December 1, 2011 20:14
-
-
Save lambacck/1419507 to your computer and use it in GitHub Desktop.
Trying out multiprocessing and reduce_socket on windows
This file contains hidden or 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
# Echo client program | |
import socket | |
HOST = 'localhost' # The remote host | |
PORT = 50007 # The same port as used by the server | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((HOST, PORT)) | |
s.send(b'Hello, world') | |
data = s.recv(1024) | |
s.close() | |
print('Received', repr(data)) |
This file contains hidden or 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 multiprocessing | |
from multiprocessing.reduction import reduce_socket | |
import socket | |
def worker(q): | |
while True: | |
(fn, args), addr = q.get() | |
conn = fn(*args) | |
print('Connected by', addr) | |
while 1: | |
data = conn.recv(1024) | |
if not data: break | |
conn.send(data) | |
conn.close() | |
if __name__=='__main__': | |
queue = multiprocessing.Queue() | |
HOST = '' | |
PORT = 50007 | |
p = multiprocessing.Process(target=worker, args=(queue,)) | |
p.start() | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((HOST, PORT)) | |
while True: | |
s.listen(1) | |
conn, addr = s.accept() | |
queue.put((reduce_socket(conn), addr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment