2012-02-09 20 views
4

我有一个简单的wsgi程序。如何从python中的simple_server在多进程中运行make_server?

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() 

因此,作为按照代码,如果是timestampEven那么它将后10秒发送响应。但是,如果timestampOdd那么它将直接发送响应而无需睡眠。

所以我的问题是如果我会发送2请求,如果第一个请求将在Even模式发送请求,那么我的第二个请求将在完成第一个请求后发送。

我检查解决方案,发现'多进程can solve this problem. I set the apache configuration with多进程. Then I get the response for奇数without completing甚至`请求。

我检查如何设置multiprocessmake_server方法simple_server模块。当我运行python /usr/lib64/python2.7/wsgiref/simple_server.py我得到的输出和最后几行是

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) 

所以我搜索了如何设置这个make_server多进程,所以如果任何请求正在进行make_server可以处理更多的则1名的请求。

Thx提前。

+2

simple_server是python中的一个玩具,如果你想真正的并发查看Tornado,Twisted或Pyramid – 2012-02-09 14:11:36

+1

我想在SOAP中创建一个web服务。所以我只需安装soaplib 2.0并使用该功能来公开我的服务。所以当我处理一个请求时,我必须等待服务器第二次请求,如果我将使用'make_server'运行服务器,所以我认为make_server不支持多进程,我是对吗? – Nilesh 2012-02-10 03:49:03

回答

2

如果你使用Apache/mod_wsgi的比你不需要的东西make_server/serve_forever。 Apache会为你处理(因为它是网络服务器)。它将处理这些进程并运行application回调函数。

确保您的Apache和mod_wsgi的配置允许多进程/多线程。好的参考可用here

+0

Thx Secator。如果我使用Apache然后它的工作,但我想使用simple_server作为可能或不可能的多进程? – Nilesh 2012-02-14 06:30:49

相关问题