2012-02-09 21 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

相關問題