[Twisted-Python] How to restart Twisted service in-process

Glyph Lefkowitz glyph at twistedmatrix.com
Tue Feb 28 19:52:16 MST 2017


> On Feb 28, 2017, at 8:11 AM, Роман Мещеряков <romanmescheryakov at yandex.ru> wrote:
> 
> Hello everyone,
> today I tried restarting twisted service using sys.execv, and it fails with the following message:
>  
> > 2017-02-28T18:39:47+0300 [ready_to_setup#debug] Before os.execv: sys.executable is /usr/bin/python and sys.argv are ['/usr/local/bin/twistd', '-ny', 'master_player.tac']
> > Another twistd server is running, PID 1939
>  
> > This could either be a previously started instance of your application or a
> > different application entirely. To start a new one, either run it in some other
> > directory, or use the --pidfile and --logfile parameters to avoid clashes.
>  
> My service is started in the following way:
>  
> PYTHONPATH=. twistd -ny master_player.tac
>  
> (i.e. without daemonization)
> Code to restart is:
>  
> > d = task.deferLater(self._reactor, seconds,
> >                     shared_code.restart_script, self._log, self._reactor)
> > # _restart_script_done should never be called
> > d.addCallback(self._restart_script_done).addErrback(self._restart_script_failure)
>  
> where shared_code.restart_script is:
>  
> > def restart_script(logger, reactor):
> >     logger.info("Restarting script...")
> >     reactor.stop()
> >     sys.stdout.flush()
> >     #TODO: force logging system to flush all log files
> >     logger.debug("Before os.execv: sys.executable is {sysexec} and sys.argv are {argv}",
> >                 sysexec = sys.executable, argv = sys.argv)
> >     os.execv(sys.executable, [sys.executable] + sys.argv)
>  
> Could someone explain to me why does the "Another twistd server is running" error occur and how can I avoid it?

twistd writes a .pid file and then attempts to check whether another process on the system has the same PID contained in that file while starting up, to avoid conflicts.

Yours is an odd case, because the process with that process-ID is the _current_ process, and perhaps the check ought to even be modified to account for this.

Two other options you have are:

switch to `twist`, which doesn't write a pidfile
use the option "--pidfile ''" to disable writing a pidfile.

I strongly suggest the first option :).

-glyph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20170228/4daee793/attachment-0002.html>


More information about the Twisted-Python mailing list