This one's bull X a little bit.
This does not involve IO, if you call gevent such as asynchronous IO or non-blocking IO, there is a large framework.
######################################################################Iterative server-webserver3a.py ## ##tested with Python 2.7.9 & python 3.4 on Ubuntu 14.04 & Mac OS X ######################################################################ImportSocketImport TimeImportOSImportSignalImporterrno server_address= (HOST, PORT) ="', 8888request_queue_size= 5defGrim_reaper (Signum, frame): whileTrue:Try: PID, status= Os.waitpid (-1, OS. Wnohang)exceptOSError:return ifPID = =0:return Print('Child {PID} terminated with status {status}'. Format (PID=pid, status=status)) defhandle_request (client_connection): Request= CLIENT_CONNECTION.RECV (1024) Print('Child pid: {pid}. Parent PID {ppid}'. Format (PID=os.getpid (), Ppid=os.getppid (),)) Print(Request.decode ()) Http_response= b"""http/1.1 OK Hello, Python wsgi! """Client_connection.sendall (http_response) time.sleep (6) defserve_forever (): Listen_socket=Socket.socket (socket.af_inet, socket. SOCK_STREAM) listen_socket.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR,1) Listen_socket.bind (server_address) Listen_socket.listen (request_queue_size)Print('serving HTTP on port {port} ...'. Format (port=PORT)) Print('Parent pid (PPID): {pid}'. Format (pid=os.getpid ())) Signal.signal (signal. SIGCHLD, Grim_reaper) whileTrue:Try: client_connection, client_address=listen_socket.accept ()exceptIOError as E:code, msg=E.argsifCode = =errno. EINTR:Continue Else: RaisePID=os.fork ()ifPID = =0:listen_socket.close () handle_request (client_connection) client_connection.close () Os._exit (0)Else: Client_connection.close ()if __name__=='__main__': Serve_forever ()
Python multi-process concurrent Web server (with Linux fork)