multiple instance on Unix

Nigel King nigel.king at orthogonsystems.com
Wed Sep 29 17:37:56 EDT 2004


Jeremy,
I have not explained very well what I wanted.

I had a program that was called randomly by specific emails arriving 
which asked for certain information. If two or more emails arrived 
simultaneously then procmail asked two or more instances of my program 
to run. These instances interfered with one another, so I needed a 
process to stop that from happening. What my son devised was for the 
first to create a directory and run and when finished to delete the 
directory. The subsequent instances could try to create a directory but 
fail in an atomic piece of code. They would sleep for 1 or more seconds 
and then try again. The first of the subsequent instances that tries 
and succeeds stops the others from succeeding.

Now, this works but I wondered whether anybody knew of a more standard 
bit of python code was available for ensuring that only one instance 
was processing. mutex does it for threads but not for instances as I 
understand it.

The specification for a better process would include the ability to 
ensure that the queue was orderly, in other words some sort of FIFO 
would ensure that first served would have been the first to request the 
lock and fail.

Our solution which does not satisfy the previous paragraph.

import os, time
try:
     # This program is not thread safe so we must protect it from being
     # trampled over by another copy
     # pause if another email is being processed for half an hour maximum
     t = time.time()+1800
     locked = True
     while locked and time.time() < t:
         try:
             os.mkdir('instancelck')
             locked = False
             pass
         except :
             time.sleep(1)
             pass
         pass
     # do everything else
     .......
finally :
     os.rmdir('instancelck')
     # Removes the thread locking device so that another copy may run
     pass

The timer was in case for any reason finally did not run successfully 
ever.

Facundo's solution I have not yet studied.

Thanks

Nigel



On 29 Sep 2004, at 18:59, Jeremy Jones wrote:

> Nigel King wrote:
>
>> Hi,
>> I have (my son has!) implemented protection against multiple 
>> instances causing havoc by creating a directory. This fails if it 
>> exists and thus in a single instruction one gets both the acquire and 
>> the test.
>>
>> Windows has it's mutex which solves the problem. Is there any better 
>> version for UNIX.
>>
>> Thanks
>>
>> Nigel King
>>
> Could you explain a little better what you're trying to do?  I'm 
> guessing that you (your son - I'm looking forward to that day, myself 
> ) have multiple Python processes and you want only one process to be 
> able to create a directory?
>
> If so, you could do something like this:
>
> import time
> import os
> waiting_for_lock = 1
>
> while waiting_for_lock:
>    try:
>        os.mkdir('/tmp/foo')
>        waiting_for_lock = 0
>    except OSError:
>        print "could not create directory"
>        time.sleep(1)
> #do whatever you need one and only one process to do...
>
> HTH
>
> Jeremy Jones
>




More information about the Python-list mailing list