Semaphore Techniques

Piet van Oostrum piet at cs.uu.nl
Thu Jul 30 09:45:44 EDT 2009


>>>>> Carl Banks <pavlovevidence at gmail.com> (CB) wrote:

>CB> On Jul 29, 7:14 am, Piet van Oostrum <p... at cs.uu.nl> wrote:
>>> >>>>> Carl Banks <pavlovevide... at gmail.com> (CB) wrote:
>>> >CB> On Jul 28, 3:15 pm, John D Giotta <jdgio... at gmail.com> wrote:
>>> >>> I'm looking to run a process with a limit of 3 instances, but each
>>> >>> execution is over a crontab interval. I've been investigating the
>>> >>> threading module and using daemons to limit active thread objects, but
>>> >>> I'm not very successful at grasping the documentation.
>>> 
>>> >>> Is it possible to do what I'm trying to do and if so anyone know of a
>>> >>> useful example to get started?
>>> >CB> It seems like you want to limit the number of processes to three; the
>>> >CB> threading module won't help you there because it deals with threads
>>> >CB> within a single process.
>>> >CB> What I'd do is to simply run the system ps to see how many processes
>>> >CB> are running (ps is pretty versatile on most systems and can find
>>> >CB> specifically targeted processes like you program), and exit if there
>>> >CB> are already three.
>>> 
>>> That will surely run into some race conditions.

>CB> What, the OS might not have gotten around to update the process table
>CB> to include a process started minutes ago?  (He said he was starting
>CB> the processes over crontab intervals, not that he communicated what he
>CB> wanted well.)

No but between the time you get the ps output and decide not to start a
new process one of the processes might have exited. As I said it
probably is not a big deal, but you (he) should be aware of it I think.

The other possible race condition: two processes starting at
approximately the same time and both not detecting the other will
probably not occur because of the time distance between starting the
processes by cron. Unless the system is so busy that ps takes a loooong
time. 

The problem is similar to the sleeping barber problem (3 sleeping
barbers actually).
-- 
Piet van Oostrum <piet at cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: piet at vanoostrum.org



More information about the Python-list mailing list