Hello everyone!

I am using python 2.6 on cygwin environment and wondering how could i prevent two python processes from writing to a file at the same time.

The file that is shared between the python processes is an ini file and is accessed through ConfigObj module.

The first python process is a daemon which runs indefinitely writing to the ini file, data on some random external conditions.

The other python processes, start occasionally (one at a time), execute their code, write data on the ini file and stop.

The command i want to ensure that is safely executed in all scripts is the :

config.write()

with config being the object that is created by the ConfigObj library :

config = ConfigObj('myfile.ini')

Testing the above, didn't produce any errors but, i can never be sure that way...

So, is there any way to do it without rewriting all my code?(Without using threads for example)

P.S. I will probably sooner or later change platform from cygwin to linux, so if there is a solution not applied in cygwin but linux compatible, i will not exlude it.

Thanks in advance.

Recommended Answers

All 5 Replies

My problem is that modules like posixfile, fcntl, etc.. need a file handle as an argument.

ConfigObj does not return a file handle, but creates an object which contains the data of the original file, and all the changes made to that object are copied back to the file only when you call :

config.write()

I am thinking of using an extra (dummy) file only to be used as a lock.
Thus, the process that acquires the lock file is the only one that can update the ini file i want.

Can i be sure that this is going to be safe that way?
Or the concurrency problem is simply transfered in the lock file?

I'm thinking that since cygwin is not SMP capable, the requests for acquiring the lock file are serialized so there will be no "real" concurrent access atempt.
If this is correct, my problem is solved...so the question now is :
"Is it correct???"

:)

Sorry i had to bump...!
:)

Anyone please..?

You should be able to use a dummy file by calling the linux command 'lockfile' from your programs (I never tried this, but it should work).

Yeah i did this.
It works fine i just wanted to be sure that using lockfile in cygwin ensures that no concurrent access will be ever possible.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.