Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
python multiprocessing socket server example
import socket
if __name__ == "__main__":
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("localhost", 9000))
data = "some data"
sock.sendall(data)
result = sock.recv(1024)
print result
sock.close()
import multiprocessing
import socket
def handle(connection, address):
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("process-%r" % (address,))
try:
logger.debug("Connected %r at %r", connection, address)
while True:
data = connection.recv(1024)
if data == "":
logger.debug("Socket closed remotely")
break
logger.debug("Received data %r", data)
connection.sendall(data)
logger.debug("Sent data")
except:
logger.exception("Problem handling request")
finally:
logger.debug("Closing socket")
connection.close()
class Server(object):
def __init__(self, hostname, port):
import logging
self.logger = logging.getLogger("server")
self.hostname = hostname
self.port = port
def start(self):
self.logger.debug("listening")
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind((self.hostname, self.port))
self.socket.listen(1)
while True:
conn, address = self.socket.accept()
self.logger.debug("Got connection")
process = multiprocessing.Process(target=handle, args=(conn, address))
process.daemon = True
process.start()
self.logger.debug("Started process %r", process)
if __name__ == "__main__":
import logging
logging.basicConfig(level=logging.DEBUG)
server = Server("0.0.0.0", 9000)
try:
logging.info("Listening")
server.start()
except:
logging.exception("Unexpected exception")
finally:
logging.info("Shutting down")
for process in multiprocessing.active_children():
logging.info("Shutting down process %r", process)
process.terminate()
process.join()
logging.info("All done")
@zakdances
Copy link

zakdances commented Aug 22, 2013

How would you rewrite this to use socketserver instead of socket?

@iamdionysus
Copy link

iamdionysus commented Sep 17, 2013

is this working? I'm having ERROR:root:Unexpected exception saying process.start() is starting the issue. It's been said passing socket to another process is not trivial thing.

@xrand3r
Copy link

xrand3r commented Jun 28, 2014

what exactly the program does??

@toannai
Copy link

toannai commented Jul 22, 2014

tks,

@cszhang
Copy link

cszhang commented Feb 3, 2015

Thank you for the code

@chenbo515
Copy link

chenbo515 commented Dec 3, 2015

It seems this code will not run under windows because multiprocessing.Process can not using os.fork(), you have to use multiprocessing.reduction to pass socket between processes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment