[Twisted-Python] Is there a recommended way for a Service to request Application stop?

Glyph Lefkowitz glyph at twistedmatrix.com
Mon Oct 10 18:02:54 MDT 2016


> On Oct 10, 2016, at 4:38 PM, Daniel Sutcliffe <dansut at gmail.com> wrote:
> 
> Generically, can I somehow bubble up events through the Service
> hierarchy, or should I communicate with external objects to the
> hierarchy that can bubble down actions from higher up?


Following the <https://en.wikipedia.org/wiki/Single_responsibility_principle <https://en.wikipedia.org/wiki/Single_responsibility_principle>>, the service hierarchy's job is just to make sure everything gets started up and shut down together.

It sounds to me like you have a pretty well-defined hierarchy which seems like it fits into the service hierarchy because it's roughly parallel in terms of which objects participate; however, you have very application-specific semantics for this parallel hierarchy.  For example, it's pretty unusual to have a super-service reconfigure a subordinate service in order to recover from an error condition, in my experience, unless you're talking about stuff like erlang supervision hierarchies, but that requires runtime support like the code being recovered running in a subprocess that doesn't share state.

It often feels like abstractions are expensive so you should have as few of them as possible; but, in reality, *simple* abstractions are cheap, and what makes abstraction expensive is when you overload them.  Make a new, simple abstraction that contains exactly the semantics you just described, and use composition to point at the appropriate point in the MultiService hierarchy.  When it's time to "stop" a service, do setServiceParent(None); when it's time to "start" it, do setServiceParent(appropriateServiceParent).  This should take care of keeping your services in the appropriate state.

BTW, if you have stateful long-running services that have to self-modify based on changing circumstances, you might want to also check out https://github.com/glyph/automat <https://github.com/glyph/automat> to see if it can help you ensure that everything's in a consistent state.

Good luck!

-glyph
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20161010/3e694983/attachment-0002.html>


More information about the Twisted-Python mailing list