2011-10-21 40 views
0

我正在使用mod_wsgi編寫WSGI應用程序。我想有很多併發連接的能力。 mod_wsgi爲每個請求創建一個新進程,使得不可能使用threading.Condition()來通知從一個線程到另一個線程的更改。跨多個進程的Python threading.Condition()功能

我知道有幾種不同的方式可以在不同的正在運行的進程(RPC,AMQP,d-bus,Jabber)之間提供實時消息傳遞,但是我特別需要的是與單線程threading.wait()和threading.notifyAll()。

當我不使用mod_wsgi時,只是運行多個線程,這裏基本上是我爲我工作的。再次

def put_value: 
    # user has given a new value 
    # update in DB, then notify any waiting threads 

    my_condition.acquire() 
    my_condition.notifyAll() 
    my_condition.release() 

def get_value: 
    # user has requested to receive a new value as of this point 
    # we will return a value as soon as we are notified it has changed 

    my_condition.acquire() 
    my_condition.wait() 
    my_condition.release() 
    # return some val out of the DB 

,我要找的是儘量靠近單行threading.wait()和threading.notifyAll()的東西:顯然,這兩個功能是由不同的線程運行。我不介意設置一些配置,甚至是在後臺運行的東西 - 但是我有相當數量的代碼,依賴於在請求中間停下來等待,直到通知它可能會繼續。

+0

mod_wsgi模塊不會爲每個請求啓動一個新進程。然而mod_wsgi的某些配置是多進程的,所以請求可能不會每次都進入相同的進程。如果你使用默認的單進程的mod_wsgi的守護進程模式,你將不會遇到這個問題。 –

回答