[BangPypers] Deploying with mod_wsgi and file locking

Noufal Ibrahim noufal at nibrahim.net.in
Mon Jun 10 19:16:45 CEST 2013

Amit Sethi <amit.pureenergy at gmail.com> writes:

> Hi All , I am writing a pyramid app and deploying it mod_wsgi . The
> file regularly writes into a file in the file system . This process of
> writing xml is in a middleware . Now the question is do I need to
> apply a file locking mechanism to make sure that multiple requests do
> not try to write to the same file . If yes what is the best way to
> implement a file locking mechanism in python.

This is going to get problematic. My first thought is to offload the
writing to a separate process which maintains a queue of such requests
and only that will write to the file. You won't have any issues
then. This might be impractical if the file needs to be updated in real
time in which case, a file is not the right way to go. You should use a
database table or something and then run some periodic process that
updates the file. 

If both of these don't work, then
http://docs.python.org/2/library/fcntl.html#fcntl.lockf exists to lock
files but I think it will really mess up the performance of your app.


More information about the BangPypers mailing list