Jim Hunziker
Jim Hunziker

Reputation: 15410

How can I control access to a shared resource when using mod_wsgi?

In mod_wsgi, web requests may be handled by python interpreters in different processes. I have a shared resource to which access needs to be synchronized. (This resource is not a database.)

Python's multiprocessing has lots of good synchronization primitives, but all seem to rely on the primitives being shared at spawn time. Since the processes are being created by mod_wsgi, I have no control over their spawning.

What's the easiest way to use something like a mutex for the shared resource?

Upvotes: 0

Views: 831

Answers (1)

Jim Hunziker
Jim Hunziker

Reputation: 15410

A couple options are posix_ipc and using file locks. I'm not thrilled about these solutions, since it seems that there should be a built-in mod_wsgi or python library approach to cross-process (without sharing a common parent process) synchronization.

Here's a file-locking implementation:

from contextlib import contextmanager
import fcntl

@contextmanager
def file_locked(filename, exclusive=True):
    lockfile = open(filename, 'w')
    if exclusive:
        fcntl.flock(lockfile, fcntl.LOCK_EX)
    else:
        fcntl.flock(lockfile, fcntl.LOCK_SH)
    yield
    fcntl.flock(lockfile, fcntl.LOCK_UN)

if __name__ == '__main__':
    from time import sleep
    from random import random
    from os import getpid
    while True:
        print '%d waiting for lock' % getpid()
        with file_locked('filelock.lck'):
            print '%d got the lock'% getpid()
            sleep(random())
        print '%d released the lock\n' % getpid()

Upvotes: 3

Related Questions