Python multi-process concurrent Web server (with Linux fork)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.