Jeff V
Jeff V

Reputation: 175

Python threading.Condition() functionality across multiple processes

I am writing a WSGI app using mod_wsgi. I want to have the ability of many concurrent connections. mod_wsgi spins up a new process for each request, making it impossible to use threading.Condition() for notifying a change from one thread to another thread.

I understand there is a few different ways to provide real time messaging between different running processes, (RPC, AMQP, d-bus, Jabber) but what I'm looking for specifically is something as close to the one-liners threading.wait() and threading.notifyAll().

When I wasn't using mod_wsgi, and just running multiple threads, here is essentially what I had working for me. Obviously the two functions were run by different threads:

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

Once again, what I'm looking for is something as close to the one-liners threading.wait() and threading.notifyAll(). I don't mind setting up some configuration and even something running in the background - but I have a fair amount of code that relies on the ability to stop and wait in the middle of a request until it is notified that it may continue.

Upvotes: 0

Views: 312

Answers (1)

Raymond Hettinger
Raymond Hettinger

Reputation: 226486

In the mod_wsgi configuration, set processes=1. See http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess for details.

Upvotes: 1

Related Questions