I have a c program which is running in thread and appending some data in a file. I want to run a python thread which will copy the same file(which c thread is writing) after some time interval. Is there any safe way to do this? I am doing this in linux OS.
Asked
Active
Viewed 120 times
0
-
One option is to lock the file: http://stackoverflow.com/questions/489861/locking-a-file-in-python – Brendan Long Apr 15 '12 at 04:18
-
Why do you need to copy the file? – Ignacio Vazquez-Abrams Apr 15 '12 at 04:19
1 Answers
0
There are a lot of important details to your scenario that aren't mentioned, but working on the assumption that you can't write a locking mechanism in to the C program and then use it in the Python program (for example, you're using an existing application on your system), you could look in to os.stat and check the last modified time, m_time. That is of course reliant on you knowing that a recent m_time means the file won't be opened again in the C program and used again.
If the file handle is kept open in the C program at all times, and written to occasionally then there is a not a lot of easy options for knowing when it is and isn't being written to.

D.j.
- 96
- 2
-
The c thread writes file after every 2 second, but my python thread is not copying the file, while for 2 second c thread release all lock from file, why? – Prakash Pandey Apr 15 '12 at 05:21
-
Why do you need to copy the file in _Python_ specifically? Why not just launch `rsync` periodically to do this? – James Youngman Apr 15 '12 at 09:39