[BangPypers] Deploying with mod_wsgi and file locking

Anand Chitipothu anandology at gmail.com
Mon Jun 10 19:23:03 CEST 2013

On Mon, Jun 10, 2013 at 10:46 PM, Noufal Ibrahim <noufal at nibrahim.net.in>wrote:

> 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.

Why not just write to database with a timestamp and run a cron job to
periodically move the recent entries from database to file that want?

That reduces the number of moving parts drastically and easy to manage.


More information about the BangPypers mailing list