This code is supposed to try and start a server process and return. If the port was taken, it should say "couldn't bind to that port" and return. If the server started, it should print "Bound to port 51231" and return. But it does not return.
import socket
from multiprocessing import Process
def serverMainLoop(s,t):
s.listen(5)
while 1:
pass # server goes here
host = ''
port = 51231
so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
so.bind((host,port))
print "Bound to port %i"%port
serverProcess = Process(target=serverMainloop, args=(so,60))
serverProcess.start()
sys.exit()
except socket.error, (value,message):
if value==98:
print "couldn't bind to that port"
sys.exit()
Is there some switch I can throw that will make multiprocessing let me do this?