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

Glyph Lefkowitz glyph at twistedmatrix.com
Mon Feb 20 02:02:14 MST 2017


> On Feb 20, 2017, at 00:58, Роман Мещеряков <romanmescheryakov at yandex.ru> wrote:
> 
>  
> Hi everyone,
>  
> The answers are below
>  
> -- 
> Kind regards, Roman Mescheryakov
>  
>  
> 19.02.2017, 02:21, "Jean-Paul Calderone" <exarkun at twistedmatrix.com <mailto:exarkun at twistedmatrix.com>>:
>> On Sat, Feb 18, 2017 at 2:25 PM, Glyph Lefkowitz <glyph at twistedmatrix.com <mailto:glyph at twistedmatrix.com>> wrote:
>>  
>>> On Feb 2, 2017, at 1:17 AM, Роман Мещеряков <romanmescheryakov at yandex.ru <mailto:romanmescheryakov at yandex.ru>> wrote:
>>>  
>>> Hello everyone! I wrote Twisted-based TCP server which is capable of running in several relatively different modes. When mode switch is needed, I would like the server to restart itself by some means, for it to read new mode from configuration file and create corresponding implementation. Also I wanted the restart to occur without re-running twistd process, because it seems to me more appropriate for Linux daemon.
>> 
>> Hi Roman,
>>  
>> Sorry it took a while to get to this one :).
>>  
>> If your solution is working for you, then it's fine.  You've correctly understood the API associated with IServiceCollection and your implementation (at least at first glance, I don't have time to test it exhaustively :)) is correct.
>>  
>> However, there's a more philosophical question as well: if you really want to re-start from scratch, what exactly are you shutting down and starting up again?  Wouldn't you want to be able to load new code?  Simply stopping and starting objects won't re-initialize the process from the beginning, only from some indeterminate middle state where parts of the program are already set up; this doesn't have the usual desired effect of a "restart" where potentially unknown or buggy state is cleared away.
>>  
>> You may want to consider simply calling execv https://docs.python.org/2.7/library/os.html#os.execv <https://docs.python.org/2.7/library/os.html#os.execv> with [sys.executable] + sys.argv rather than trying to do this in terms of Twisted's APIs.
>  
> You are right, Glyph, using os.execv would be more simple, reliable and straightforward way of achieving my goal. The only thing that bothers me due to lack of Linux experience: is it OK to os.execv inside daemon process? Because I run my process using twistd, not python directly.

If you're actually daemonizing ('twistd' without '-n') then there might be a small issue.  However, a modern best practice is to never daemonize; this is why the new `twist` runner (which you should probably switch to) doesn't daemonize and expects your supervisor to handle backgrounding.

>>  
>> Or better yet.  Don't be afraid to just exit.  Your process is being managed by a supervisor with a rich feature set - like easily controlled re-restart behavior, right (If not, what do you do when your daemon simply crashes)?
>>  
> 
> Well, in fact I'm in the development stage now and did't have time to deal with this question yet :) As of now I understand very little about daemons and their supervisors (I belive it is systemd for Raspbian...).

Yes, systemd will do this.

> Jean-Paul and Glyph, thank you for guiding me in the right direction! :)
>  
>  

Happy to help!

-g

>> Jean-Paul
>>  
>> ,
>> _______________________________________________
>> Twisted-Python mailing list
>> Twisted-Python at twistedmatrix.com <mailto:Twisted-Python at twistedmatrix.com>
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python <http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python>_______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com <mailto:Twisted-Python at twistedmatrix.com>
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python <http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20170220/02518b85/attachment-0002.html>


More information about the Twisted-Python mailing list