[Twisted-Python] Twisted and NT Services
At $FIRM, a good number of processes around the place are Python scripts using Twisted. Since they're functionally equivalent to Unix daemons, what's the best way of turning a Twisted script into a service? Also, I've gotten into the habit of using py2exe to turn scripts into standalone Win32 programs, and it would be really nice to be able to ship a standalone NT service. Unfortunately, there seems to be a "pick any two" relationship between Twisted, NT Services, and py2exe - when I try and combine all three, I get the following traceback inside my Application.run() call: File "twisted\internet\app.pyc", line 861, in run --- <exception caught here> --- File "twisted\python\log.pyc", line 64, in callWithLogger File "twisted\python\log.pyc", line 51, in callWithContext File "twisted\python\context.pyc", line 32, in callWithContext File "twisted\internet\default.pyc", line 122, in run File "twisted\internet\default.pyc", line 116, in startRunning File "twisted\internet\default.pyc", line 88, in _handleSignals exceptions.ValueError: signal only works in main thread If, in the Python source which contains the Service subclass, I put (at the top): from twisted.internet import win32eventreactor win32eventreactor.install() ...and then make it into a service, I get the same error (Entertainingly, if I put it *not* at the top, when I run the resulting exe I get the entertaining dialog box "Could not get Script info"). As you might have gathered, I'm not using .taps for these services - although I suspect they'd meet the 'don't reveal our source code to random people' requirement, I suspect they wouldn't help with the issue of installing Python and half a dozen extension modules on every server. Anyone have any hints or suggestions? -- ___________ ____________________________ | Screwtape | Reply-To: munged on Usenet |________ ______ ____ __ _ _ _ | | Nice sensible hobbits stay with Sméagol. |
On Tue, 24 Jun 2003 11:31:53 +1000 screwtape@froup.com wrote:
At $FIRM, a good number of processes around the place are Python scripts using Twisted. Since they're functionally equivalent to Unix daemons, what's the best way of turning a Twisted script into a service?
Well, ideally we'd want some equivalent of twistd that runs an NT service, so you could write a python script that's runnable with "twistd -y" or a TAP or whatever and do that. And then presumably you would write the equivalent of tap2deb (tap2msi, probably) so you could easily install and uninstall them. No one's ever written one, though, for lack of motivation. If you'd write this and submit it I'd love to have this in Twisted, which along with a decent win32 reactor would give us really good win32 support. Or you could pay someone to do it. Or, and this is what everyone else has done, write a few mini-scripts that run your NT services and that's it. See list archives for code that will start a NT service. This is of course the fastest route. -- Itamar Shtull-Trauring http://itamarst.org/ http://www.zoteca.com -- Python & Twisted consulting
On Tue, Jun 24, 2003 at 11:31:53AM +1000, screwtape@froup.com wrote: [...]
Also, I've gotten into the habit of using py2exe to turn scripts into standalone Win32 programs, and it would be really nice to be able to ship a standalone NT service. Unfortunately, there seems to be a "pick any two" relationship between Twisted, NT Services, and py2exe - when I try and combine all three, I get the following traceback inside my Application.run() call:
File "twisted\internet\app.pyc", line 861, in run --- <exception caught here> --- File "twisted\python\log.pyc", line 64, in callWithLogger File "twisted\python\log.pyc", line 51, in callWithContext File "twisted\python\context.pyc", line 32, in callWithContext File "twisted\internet\default.pyc", line 122, in run File "twisted\internet\default.pyc", line 116, in startRunning File "twisted\internet\default.pyc", line 88, in _handleSignals exceptions.ValueError: signal only works in main thread
Try passing installSignalHandlers=0 to Application.run to work around this. -Andrew.
On Tue, Jun 24, 2003 at 12:54:32PM +1000, Andrew Bennetts wrote:
On Tue, Jun 24, 2003 at 11:31:53AM +1000, screwtape@froup.com wrote: [...]
Also, I've gotten into the habit of using py2exe to turn scripts into standalone Win32 programs, and it would be really nice to be able to ship a standalone NT service. Unfortunately, there seems to be a "pick any two" relationship between Twisted, NT Services, and py2exe - when I try and combine all three, I get the following traceback inside my Application.run() call:
File "twisted\internet\app.pyc", line 861, in run --- <exception caught here> --- File "twisted\python\log.pyc", line 64, in callWithLogger File "twisted\python\log.pyc", line 51, in callWithContext File "twisted\python\context.pyc", line 32, in callWithContext File "twisted\internet\default.pyc", line 122, in run File "twisted\internet\default.pyc", line 116, in startRunning File "twisted\internet\default.pyc", line 88, in _handleSignals exceptions.ValueError: signal only works in main thread
Try passing installSignalHandlers=0 to Application.run to work around this.
Out of curiosity, does Win32 even have signal handlers? Or are these signal handlers something entirely different from the Unix concept? -- ___________ ____________________________ | Screwtape | Reply-To: munged on Usenet |________ ______ ____ __ _ _ _ | | ... that every day's a rerun and the laughter's always canned. |
On Tue, Jun 24, 2003 at 02:07:13PM +1000, screwtape@froup.com wrote:
On Tue, Jun 24, 2003 at 12:54:32PM +1000, Andrew Bennetts wrote:
On Tue, Jun 24, 2003 at 11:31:53AM +1000, screwtape@froup.com wrote: [...]
exceptions.ValueError: signal only works in main thread
Try passing installSignalHandlers=0 to Application.run to work around this.
Out of curiosity, does Win32 even have signal handlers? Or are these signal handlers something entirely different from the Unix concept?
Python does have a signal module on Win32, although I'm not sure what the underlying Win32 API supports. twisted.internet.default.PosixReactorBase._handleSignals might be instructive, though: def _handleSignals(self): """Install the signal handlers for the Twisted event loop.""" import signal signal.signal(signal.SIGINT, self.sigInt) signal.signal(signal.SIGTERM, self.sigTerm) # Catch Ctrl-Break in windows (only available in Python 2.2 and up) if hasattr(signal, "SIGBREAK"): signal.signal(signal.SIGBREAK, self.sigBreak) if platform.getType() == 'posix': signal.signal(signal.SIGCHLD, self._handleSigchld) -Andrew.
On Tue, Jun 24, 2003 at 12:54:32PM +1000, Andrew Bennetts wrote:
On Tue, Jun 24, 2003 at 11:31:53AM +1000, screwtape@froup.com wrote: [...]
Also, I've gotten into the habit of using py2exe to turn scripts into standalone Win32 programs, and it would be really nice to be able to ship a standalone NT service. Unfortunately, there seems to be a "pick any two" relationship between Twisted, NT Services, and py2exe - when I try and combine all three, I get the following traceback inside my Application.run() call:
File "twisted\internet\app.pyc", line 861, in run --- <exception caught here> --- File "twisted\python\log.pyc", line 64, in callWithLogger File "twisted\python\log.pyc", line 51, in callWithContext File "twisted\python\context.pyc", line 32, in callWithContext File "twisted\internet\default.pyc", line 122, in run File "twisted\internet\default.pyc", line 116, in startRunning File "twisted\internet\default.pyc", line 88, in _handleSignals exceptions.ValueError: signal only works in main thread
Try passing installSignalHandlers=0 to Application.run to work around this.
-Andrew.
For any lurking FAQ maintainers, that seemed to do the trick quite nicely. I lose the ability to kill a service with ^C, but you can't really send a ^C to a service anyway. If I have time in the near future, I'll try and have a look at what twistd does, what NT services need, and whether the two can be made to meet. -- ___________ ____________________________ | Screwtape | Reply-To: munged on Usenet |________ ______ ____ __ _ _ _ | | I don't lick skyscrapers. |
participants (3)
-
Andrew Bennetts
-
Itamar Shtull-Trauring
-
screwtape@froup.com