Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Python 3.5 amd64 and win32service

493 views
Skip to first unread message

Nagy László Zsolt

unread,
Oct 3, 2016, 9:26:36 AM10/3/16
to

Hello,

Is it possible to write a win32 service with 64 bit python 3.5? The
pywin32 package does exist on 3.5 64bit, but missing some modules:

>>> import win32service
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: DLL load failed:The specified module could not be found.

>>>

Missing modules: win32service win32event servicemanager

I have also tried the 32 bit version of Python. The 32 bit version does
have the missing modules, but the installer fails. Even if I start it
as a system admin, I get this message:

LoadLibraryEx returned 1444544512, last error 0

The basic question: is it - at least in theory - possible to use 64 bit
python in a win32 service?

Thanks,

Laszlo


eryk sun

unread,
Oct 3, 2016, 9:42:01 AM10/3/16
to
On Mon, Oct 3, 2016 at 1:26 PM, Nagy László Zsolt <gan...@shopzeus.com> wrote:
>
> Is it possible to write a win32 service with 64 bit python 3.5? The
> pywin32 package does exist on 3.5 64bit, but missing some modules:

Try pip installing the "pypiwin32" package.

Nagy László Zsolt

unread,
Oct 4, 2016, 3:33:35 AM10/4/16
to


>> Is it possible to write a win32 service with 64 bit python 3.5? The
>> pywin32 package does exist on 3.5 64bit, but missing some modules:
> Try pip installing the "pypiwin32" package.
That worked, thanks.

Do you have an explanation why to official build (
https://sourceforge.net/projects/pywin32/files/pywin32/ ) is missing
these module? I'm curious.

After installing this package, the "service start" operation throws "The
Service Did Not Respond To The Start Or Control Request In A Timely
Fashion". error code 1053

I have written many other win32 services in python2 before, but never
got this error message. Is this specific to windows 10?

Here is an MWE:

#!/usr/bin/python3
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import threading
import time


class AppServerSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"

def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.stop_requested = threading.Event()
self.stop_requested.clear()
socket.setdefaulttimeout(60)

def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
self.ReportServiceStatus(win32service.SERVICE_STOPPED)

def main(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
while not self.stop_requested.is_set():
time.sleep(1) # So something useful here


if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)







eryk sun

unread,
Oct 4, 2016, 9:31:15 AM10/4/16
to
On Tue, Oct 4, 2016 at 7:33 AM, Nagy László Zsolt <gan...@shopzeus.com> wrote:
>
>>> Is it possible to write a win32 service with 64 bit python 3.5? The
>>> pywin32 package does exist on 3.5 64bit, but missing some modules:
>> Try pip installing the "pypiwin32" package.
> That worked, thanks.
>
> Do you have an explanation why to official build (
> https://sourceforge.net/projects/pywin32/files/pywin32/ ) is missing
> these module?

Not off hand.

> After installing this package, the "service start" operation throws "The
> Service Did Not Respond To The Start Or Control Request In A Timely
> Fashion". error code 1053

Run the post-install script (in the Scripts folder):
"pywin32_postinstall.py -install". First edit the script to replace
references to "pywin32_system32" with "pypiwin32_system32", which
reflects the changed directory name in the wheel package. Running this
script should copy over pywintypes35.dll and pythoncom35.dll to the
System32 directory. pywintypes35.dll is required by PythonService.exe.

> def main(self):
> self.ReportServiceStatus(win32service.SERVICE_RUNNING)
> while not self.stop_requested.is_set():
> time.sleep(1) # So something useful here

Why don't you use the Windows Event (hWaitStop) with
WaitForSingleObject instead of an additional threading.Event?

Nagy László Zsolt

unread,
Oct 5, 2016, 2:18:32 AM10/5/16
to

>> def main(self):
>> self.ReportServiceStatus(win32service.SERVICE_RUNNING)
>> while not self.stop_requested.is_set():
>> time.sleep(1) # So something useful here
> Why don't you use the Windows Event (hWaitStop) with
> WaitForSingleObject instead of an additional threading.Event?

The main question is: why does this work? What should be different?

About why I use a threading Event instead instead of a Windows event -
production code is similar to this:

def main(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
worker = Worker(self.stop_requested)
worker.join()


The worker class does the real job. The win32 service code is only a
wrapper around it. The service can also be started from unix (with an rc
script). In the unix style application, there is another wrapper that
sets up signal handlers, writes out a pid file and starts the same
worker object. The worker must work on both platforms, this is why I
must use platform independent object.

But again, that is not related to the question. Why does it not work?
What is missing?

Thanks,

Laszlo




eryk sun

unread,
Oct 5, 2016, 2:32:52 AM10/5/16
to
On Wed, Oct 5, 2016 at 6:18 AM, Nagy László Zsolt <gan...@shopzeus.com> wrote:
> But again, that is not related to the question. Why does it not work?
> What is missing?

If you mean why running the service doesn't work, it should once you
run the post-install script that copies pywintypes35.dll to the
Windows System32 directory. This DLL is required by PythonService.exe.
It fails to start without it. After running the post-install script,
run `where pywintypes35.dll` to verify that it was copied correctly.

Nagy László Zsolt

unread,
Oct 5, 2016, 3:43:39 AM10/5/16
to

>> But again, that is not related to the question. Why does it not work?
>> What is missing?
> If you mean why running the service doesn't work, it should once you
> run the post-install script that copies pywintypes35.dll to the
> Windows System32 directory. This DLL is required by PythonService.exe.
> It fails to start without it. After running the post-install script,
> run `where pywintypes35.dll` to verify that it was copied correctly.
It did not help. I still get the same error message (service did not
respond in time).

By the way, running the aforementioned post install script raised this
error:

C:\Users\User\appdata\Local\programs\Python\Python35\Scripts>pywin32_postinstall.py
-install
Traceback (most recent call last):
File
"C:\Users\User\appdata\Local\programs\Python\Python35\Scripts\pywin32_postinstall.py",
line 6, in <module>
import winreg as winreg
ImportError: No module named winreg

In addition to the replacements, I had to do "py -3
pywin32_postinstall.py -install". The package developer should add
"#!/usr/bin/python3" to the beginning of that post install script, so
that py.exe can select the right interpreter automatically.

In my opinion, the pypiwin32 and pywin32 packages are both broken. I say
this because I cannot just "install the package". The pywin32 installer
throws a cryptic error message. Pypiwin32: I have to edit source code
files and figure out command lines that are not documented. Not hard to
do for me, but still...


eryk sun

unread,
Oct 5, 2016, 8:04:40 AM10/5/16
to
On Wed, Oct 5, 2016 at 7:43 AM, Nagy László Zsolt <gan...@shopzeus.com> wrote:
> It did not help. I still get the same error message (service did not
> respond in time).

Can you run the service executable directly? From the command line
check `sc qc TestService`. Run the BINARY_PATH_NAME executable, e.g.

for /f "tokens=1* delims=: " %i in (
'sc qc TestService ^| findstr BINARY_PATH_NAME') do @%j

output:

C - Python Service Manager
Options:
-register - register the EXE - this should generally not be necessary.
-debug servicename [parms] - debug the Python service.

NOTE: You do not start the service using this program - start the
service using Control Panel, or 'net start service_name'

Nagy László Zsolt

unread,
Oct 6, 2016, 1:23:15 AM10/6/16
to
All of the commands below were executed in cmd.exe running in elevated
mode (as administrator):

c:\Users\Laci\Python\Projects\gateway>service_test.py install
Installing service TestService
Service installed

c:\Users\Laci\Python\Projects\gateway>sc qc TestService
[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: TestService
TYPE : 10 WIN32_OWN_PROCESS
START_TYPE : 3 DEMAND_START
ERROR_CONTROL : 1 NORMAL
BINARY_PATH_NAME :
"C:\Users\Laci\AppData\Local\Programs\Python\Python35\lib\site-packages\win32\PythonService.exe"
LOAD_ORDER_GROUP :
TAG : 0
DISPLAY_NAME : Test Service
DEPENDENCIES :
SERVICE_START_NAME : LocalSystem



It seems to be the correct executable:

c:\Users\Laci\Python\Projects\gateway>py -3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'C:\\Users\\Laci\\AppData\\Local\\Programs\\Python\\Python35\\python.exe'

The service starts to fail in any way:

* service_test.py start
* net start testservice
* from the services.msc with the start button

The error message suggest that the service does not respont to the start
request.

The MWE I provided is so simple. By now, It should be obvious what is
missing from it. :-(


Nagy László Zsolt

unread,
Oct 6, 2016, 1:34:14 AM10/6/16
to

>
> The MWE I provided is so simple. By now, It should be obvious what is
> missing from it. :-(
>
>
The problem is NOT with my code. I just installed a new virtual machine,
and installed python3.5 amd64 + pypiwin32 on it. On that machine, the
test service works perfectly!

So it is with my machine. My machine has Python2.7 32bit, Python 3.5
32bit and Python 3.5 64bit installed. Can this be the problem? But how?

What to do next?

eryk sun

unread,
Oct 6, 2016, 6:49:24 AM10/6/16
to
On Thu, Oct 6, 2016 at 5:23 AM, Nagy László Zsolt <gan...@shopzeus.com> wrote:
> "C:\Users\Laci\AppData\Local\Programs\Python\Python35\lib\site-packages\win32\PythonService.exe"

I wanted you to run the above executable, not python.exe. If it fails
you'll get more information about why it's failing when run directly
then when the service controller runs it. Since you're using a
per-user installation of Python 3.5, possibly it can't find
python35.dll.

Nagy László Zsolt

unread,
Oct 7, 2016, 2:37:50 AM10/7/16
to

>> "C:\Users\Laci\AppData\Local\Programs\Python\Python35\lib\site-packages\win32\PythonService.exe"
> I wanted you to run the above executable, not python.exe. If it fails
> you'll get more information about why it's failing when run directly
> then when the service controller runs it. Since you're using a
> per-user installation of Python 3.5, possibly it can't find
> python35.dll.
This is something I cannot understand. This is what happens when I start
it from the command line, as a normal user:

C:\>"C:\Users\Laci\AppData\Local\Programs\Python\Python35\lib\site-packages\win32\PythonService.exe"
-debug TestService
Debugging service TestService - press Ctrl+C to stop.
Info 0x40001002 - The TestService service has started.


*Meanwhile, the service manager shows that the service is not started*.

Then I press Ctrl+C several times and I see this:

C:\>"C:\Users\Laci\AppData\Local\Programs\Python\Python35\lib\site-packages\win32\PythonService.exe"
-debug TestService
Debugging service TestService - press Ctrl+C to stop.
Info 0x40001002 - The TestService service has started.
Stopping debug service.
Stopping debug service.
Stopping debug service.
Stopping debug service.
Stopping debug service.

But the process is not closed. I had to terminate pythonservice.exe from
task manager. Next thing I tried is to start it as an administrator:
there is no difference. On the console it says that it has been started,
but in service manager it appears to be stopped.

I got no messages about missing DLL files at all.

The very same MWE works perfectly on a new virtual machine with Python
3.5.2 amd64 + pypiwin32.

Any idea what could be wrong?





0 new messages