How to run make_server from simple_server in python in multiprocessor mode?

I have one simple wsgi program.

from wsgiref.simple_server import make_server import time def application(environ, start_response): response_body = 'Hello World' status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))] start_response(status, response_headers) if environ['PATH_INFO'] != '/favicon.ico': print "Time :", int(time.time()) if int(time.time()) % 2: print "Even" time.sleep(10) else: print "Odd" return [response_body] httpd = make_server('localhost', 8000, application) httpd.serve_forever() 

So, according to the code, if timestamp is Even , then it will send a response in 10 seconds. But if timestamp is Odd , then it will send the response directly without sleep.

So my question is: if I send request 2, and if the first request sends the request in Even mode, then my second request will be served after the first is completed.

I check the solution and find that "multiprocessor can solve this problem. I set the apache configuration with multiprocessor . Then I get the response for odd without completing Even` request.

I am checking how to set multiprocess using the make_server method of make_server module. When I run python /usr/lib64/python2.7/wsgiref/simple_server.py , I get the output and the last few lines

 wsgi.errors = <open file '<stderr>', mode 'w' at 0x7f22ba2a1270> wsgi.file_wrapper = <class wsgiref.util.FileWrapper at 0x1647600> wsgi.input = <socket._fileobject object at 0x1569cd0> wsgi.multiprocess = False wsgi.multithread = True wsgi.run_once = False wsgi.url_scheme = 'http' wsgi.version = (1, 0) 

So, I was looking for how to set this make_server multiprocessor, so make_server can handle more than 1 request if any request is executed.

thanks in advance.

+4
source share
1 answer

If you use Apache / mod_wsgi, you do not need make_server/serve_forever . Apache will handle this for you (since it is a web server). It will process the processes and run the application callback function.

Ensure that the configuration of Apache and mod_wsgi allows multi-processor / multi-threading. Good link is available here.

+2
source

Source: https://habr.com/ru/post/1395562/


All Articles