views:

52

answers:

2

Hi,

I want to read and write python-source-files from the file system in a thread-safe way.

open("n2.py","w").write(my_new_py_class)
from myproject import n2
#do something with n2

I assume that this is not thread-safe, since a request2 could modify the file before request1 is loading and executing it. I would like to achieve something like that one thread is waiting till the other thread wrote, loaded, executed and closed the file.

+3  A: 

Why are you making your application modify its own files? This is not only incredibly evil, metaprogramming is orders of magnitude harder to understand debug. Plus, python caches modules it imports, so it's not really easy to reload that. And, last but not least, you don't have to writ the code to a file to execute it, if you really must execute dynamically generated code.

To answer your question about writing files in a thread safe way, the general convention is to:

  • Write your content to a temporary file on the same filesystem as your target file.
  • Rename that temporary file to your target file, overwritting it in the process.

This works, because rename is atomic on POSIX systems, when done on the same device. So other threads/processes will either still have the old file opened, which is now detached from the filesystem and will be deleted as soon as those threads/processes are done with it, or they will open the new file with all of its contents. You avoid having a file that is only half-written.

In practice, I like to make a temporary directory with python's tempfile module, and write the file in there, then move it and remove the directory -- this way the file is being created with default umask.

Last but not least, rename is not really atomic on Windows, at least with default settings, as it won't let you overwrite your old file -- you need to do two renames, and that introduces a possibility of race condition. I don't know a good solution for Windows.

Radomir Dopieralski
I want to modify my own files because of a task that I have to solve that I described in the following thread: http://stackoverflow.com/questions/3590166/instantiate-python-class-from-class-available-as-string-only-in-memoryThanks for your explanation. I will try to translate it into code. Not sure I'm able to do it ;-) And the django app is not running on windows.
Tom Tom
Would you still recommend your approach for the given pdf-merging task?
Tom Tom
It feels really dirty, and I'm pretty sure that importing things is not really thread-safe. At the very least, use the runpy module (http://docs.python.org/library/runpy.html). Remember to save your files with random names, so that two threads/processes don't step on each other's toes. I'm sure there should be some way of running that code as module without writing it to file, but can't find anything: the imp module requires real files, exec won't give you a module... Are you sure there is no better way in reportlab to do the merging?
Radomir Dopieralski
I did a lot of searching and all I did come up with was this one: (http://two.pairlist.net/pipermail/reportlab-users/2006-May/004940.html), saying that there is no other way than the file approach. Yes definitely it feels dirty. But I might give the runpy module a try. With random names it could do the trick. Still would be great to run the code as a module without writing it to a file. But also did not find anything. Thanks for your effort.
Tom Tom
A: 

I had a similar problem. Check the Erik Karulf answer in this question:

http://stackoverflow.com/questions/3514569/django-and-dynamically-generated-images

We also provided some code :)

Cesar Canassa