[Twisted-web] wsgi thread synchronization

Christopher Armstrong radix at twistedmatrix.com
Fri Jul 27 11:49:37 EDT 2007

On 7/27/07, Phil Christensen <phil at bubblehouse.org> wrote:

>      host_tree = {}
>      host_tree_lock = threading.BoundedSemaphore
>      def get_application(env):
>          global host_tree, host_tree_lock
>          host = env.get('HTTP_HOST', env['SERVER_NAME'])
>          host_tree_lock.acquire()

[bunch of stuff]

>          host_tree_lock.release()
>          return app

> The only variable shared between threads is the `host_tree` global
> (the `env` variable is the WSGI environment, which is unique per
> thread, and the resulting `app` object is immutable). This works for
> me and is sufficient for my needs, but I'd appreciate a sanity check
> from anyone with some thread karma to spare.

The most immediately obvious problem is that you're not protecting
yourself from an error in the code between the acquire and the
release. You'll want to start a try: block immediately after the
acquire and move the release into a finally: block.

Christopher Armstrong
International Man of Twistery

More information about the Twisted-web mailing list