multiple instance on Unix

Mike Meyer mwm at
Thu Sep 30 05:02:40 CEST 2004

Nigel King <nigel.king at> writes:

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

The (old) standard method for locking on Unix is to use a file. Open
it in exclusive mode. Using<filename>, O_EXCL | O_RDONLY) (or
O_WRONLY, or O_RDWR) should do the trick.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list