avoid script running twice

Evan Klitzke evan at yelp.com
Mon Jun 18 13:44:48 EDT 2007


On 6/18/07, Tim Williams <tim at tdw.net> wrote:
> On 18/06/07, Evan Klitzke <evan at yelp.com> wrote:
> > On 6/18/07, Robin Becker <robin at reportlab.com> wrote:
> > > I wish to prevent a python script from running twice; it's an hourly job, but
> > > can take too long.
> > >
> > > My simplistic script looks like
> > >
> > >
> > > .......
> > > def main():
> > >      fn = 'MARKER'
> > >      if os.path.isfile(fn):
> > >          log('%s: hourly job running already' % formatTime())
> > >      else:
> > >          f = open(fn,'w')
> > >          f.write(str(os.getpid()))
> > >          f.close()
> > >          try:
> > >             work()
> > >          finally:
> > >              os.remove(fn)
> > >
> > > if __name__=='__main__':
> > >      main()
> > >
> > > but it occurs to me that I might be killed with prejudice during the long
> > > running work(). Is there a smart way to avoid running simultaneously.
> >
> > Another method that you can use is to open up a socket on some
> > predetermined port (presumably above 1024), and then have your program
> > try to connect to that port and "talk" to the other program to
> > determine whether or not to run (or whether to do some of the
> > remaining work, etc.).
>
> You don't need to talk to the socket, a second script trying to create
> a second socket on the same number will throw an exception and you can
> exit the script cleanly without running a second copy.

Just trying to bind to the port is the simplest thing to do if all you
want to do is test if another instance of the script is running,
because as Tim pointed out, you cannot bind to a port that another
program is using. But there's also an advantage to spending the time
to actual set up some sort of communication protocol using the port
for two reasons: 1) if another program binds to the port, you'll be
able to detect this and 2) it may be advantageous to split up the work
among two (or more) processes, and you can do synchronization among
the processes by having them communicate over the socket. For example,
it may be the case that the first process can tell the second instance
of the script "I'm almost done, if you want to help you can work on
this part of the remaining problem."


-- 
Evan Klitzke <evan at yelp.com>



More information about the Python-list mailing list