PyInstaller + Pywin32 windows services

2,864 views
Skip to first unread message

KyleFlavin

unread,
Mar 27, 2012, 12:36:44 AM3/27/12
to PyInstaller
I apologize -- I opened a ticket for this, and probably should have
posted to the mailing list first. I'm wondering if anyone else has
bumped into this issue, and if I'm missing something obvious.

I am attempting to make a python windows service using the pywin32
libraries. The service works when I install it using pywin32's
PythonService.exe. It also seems to work (at least on the surface)
when I make an exe using pyinstaller.

When using pyinstaller, I'm able to install and start my service
without an issue. When I shut down the service, the application
appears to stop as well. However, I get a strange error on the
console (using win32's win32traceutil.py), as follows:

"Exception KeyError: KeyError(2244,) in <module 'threading' from
'2\build\pyi.win32\agentservice\outPYZ1.pyz/threading'> ignored"

When I strip my service down to its most basic components, I find that
simply importing the python logging module is causing the error. The
error only occurs on shut down (so the import itself doesn't throw an
exception). No errors are logged to the windows event log.


My setup:
- Python 2.6.6
- Pyinstaller 1.5.1
- Pywin32 build 217
- Windows XP

Here's what my code looks like. If the logging module below (in
SvcDoRun) is commented out, the error disappears.

-------------------------------------------------------

import win32service
import win32serviceutil
import win32event
import win32evtlogutil
import win32traceutil
import servicemanager
import sys
import os
import time

class myservice(win32serviceutil.ServiceFramework):
_svc_name_ = "5"
_svc_display_name_ = "5"
_svc_deps_ = ["EventLog"]

def __init__(self, args):
self.isAlive = True
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

def event_log(self, msg):
servicemanager.LogInfoMsg(str(msg))

def SvcStop(self):
# tell Service Manager we are trying to stop (required)
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)

print "svcstop: stopping service, setting event"
# set the event to call
win32event.SetEvent(self.hWaitStop)

print "svcstop: ending svcstop"

def SvcDoRun(self):
print "we are starting the service..."
self.event_log("Starting %s" % self._svc_name_)


############ IF LOGGING IS COMMENTED OUT, THE ERROR GOES
AWAY################
import logging

print "svcdorun: waiting for object"

win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)
print "svcdorun: return from function"

if __name__ == '__main__':
if len(sys.argv)==1:
import win32traceutil
print "service is starting..."

#servicemanager.Initialize()
servicemanager.Initialize('backup service', None)
servicemanager.PrepareToHostSingle(myservice)
# Now ask the service manager to fire things up for us...
servicemanager.StartServiceCtrlDispatcher()

print "service done!"
else:
win32serviceutil.HandleCommandLine(myservice)

Hartmut Goebel

unread,
Mar 28, 2012, 4:43:21 AM3/28/12
to pyins...@googlegroups.com
Am 27.03.2012 06:36, schrieb KyleFlavin:
> Here's what my code looks like. If the logging module below (in
> SvcDoRun) is commented out, the error disappears.

Does this error alos occur if you put the import statement into the
global section of the module?

--
Schönen Gruß - Regards
Hartmut Goebel
Dipl.-Informatiker (univ.), CISSP, CSSLP

Goebel Consult
Spezialist für IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de

Monatliche Kolumne: http://www.cissp-gefluester.de/
Goebel Consult ist Mitglied bei http://www.7-it.de


KyleFlavin

unread,
Apr 2, 2012, 2:18:20 AM4/2/12
to PyInstaller
No, it doesn't occur when I move the import into the global section of
the module. I'm wondering why that'd make a difference though.

I also attempted to run the same code on a freshly installed machine.
The error disappeared, for awhile, and then reappeared sometime
later. I can't pinpoint exactly what caused it to reoccur, but I
wonder if it has something to do with me mixing the service installer
for the PythonService.exe wrapper with the Pyinstaller EXE. In other
words, sometimes I'll install the service like this for testing
purposes:

> python service.py install
> python service.py start
> python service.py stop

which installs/runs my code as a Windows services using the
PythonService.exe wrapper. I do not see the threading error when I do
this.

And sometimes I'll install using the Pyinstaller EXE after building
it:

> service.exe install
> service.exe start
> service.exe stop

Once the error begins to occur, I can't get rid of it; even if I
remove the service, reboot, and reinstall the service again. Maybe
this isn't a critical issue -- I only see the error in
Win32traceutil.py, not in the event log. If it's harmless, I'll
ignore it. I'm just not sure if that's the case.


On Mar 28, 1:43 am, Hartmut Goebel <h.goe...@goebel-consult.de> wrote:
> Am 27.03.2012 06:36, schrieb KyleFlavin:
>
> > Here's what my code looks like.  If the logging module below (in
> > SvcDoRun) is commented out, the error disappears.
>
> Does this error alos occur if you put the import statement into the
> global section of the module?
>
> --
> Schönen Gruß - Regards
> Hartmut Goebel
> Dipl.-Informatiker (univ.), CISSP, CSSLP
>
> Goebel Consult
> Spezialist für IT-Sicherheit in komplexen Umgebungenhttp://www.goebel-consult.de
>
> Monatliche Kolumne:http://www.cissp-gefluester.de/
> Goebel Consult ist Mitglied beihttp://www.7-it.de
>
>  smime.p7s
> 6KViewDownload

Hartmut Goebel

unread,
Apr 2, 2012, 4:54:46 AM4/2/12
to pyins...@googlegroups.com
Am 02.04.2012 08:18, schrieb KyleFlavin:
> No, it doesn't occur when I move the import into the global section of
> the module. I'm wondering why that'd make a difference though.

runSrv most probably is run in a separate thread (or such). So this may
be a threading issue.

> Once the error begins to occur, I can't get rid of it; even if I
> remove the service, reboot, and reinstall the

Even if importing logging globally?

KyleFlavin

unread,
Apr 3, 2012, 1:15:02 AM4/3/12
to PyInstaller
The error goes away when importing globally. At least, I haven't been
able to duplicate the issue yet. I'm not sure I understand why that
is fixing the problem though. Why would a global import be different?

On Apr 2, 1:54 am, Hartmut Goebel <h.goe...@goebel-consult.de> wrote:
> Am 02.04.2012 08:18, schrieb KyleFlavin:
>
> > No, it doesn't occur when I move the import into the global section of
> > the module.  I'm wondering why that'd make a difference though.
>
> runSrv most probably is run in a separate thread (or such). So this may
> be a threading issue.
>
> > Once the error begins to occur, I can't get rid of it; even if I
> > remove the service, reboot, and reinstall the
>
> Even if importing logging globally?
>
> --
> Schönen Gruß - Regards
> Hartmut Goebel
> Dipl.-Informatiker (univ.), CISSP, CSSLP
>
> Goebel Consult
> Spezialist für IT-Sicherheit in komplexen Umgebungenhttp://www.goebel-consult.de
>
> Monatliche Kolumne:http://www.cissp-gefluester.de/
> Goebel Consult ist Mitglied beihttp://www.7-it.de
>
>  smime.p7s
> 6KViewDownload

Hartmut Goebel

unread,
Apr 3, 2012, 3:28:24 AM4/3/12
to pyins...@googlegroups.com
Am 03.04.2012 07:15, schrieb KyleFlavin:
> The error goes away when importing globally. At least, I haven't been
> able to duplicate the issue yet. I'm not sure I understand why that
> is fixing the problem though. Why would a global import be different?

Do repeat myself: this may be a threading issue in SrvDoRun.

BTW: Your code differs from the recipes I've found:
http://code.activestate.com/recipes/576451-how-to-create-a-windows-service-in-python/
http://timgolden.me.uk/python/win32_how_do_i/track-session-events.html

Reply all
Reply to author
Forward
0 new messages