为什么 pywin32 服务没有正确停止?
Why is pywin32 service not stopping correctly?
我想 运行 一个 websocket 服务器作为 windows 服务。
我能够通过从 Python 角落复制和修改用于创建服务的代码来创建一个简单的回显服务器:
https://www.thepythoncorner.com/2018/08/how-to-create-a-windows-service-in-python/
我使用了这里的代码:
https://websockets.readthedocs.io/en/stable/intro.html
创建 websocket 回显服务器。
我可以安装服务:
python create_service.py install
启动服务:
python create_service.py start
套接字服务器运行没有问题。但是当我尝试像这样停止服务时:
python create_service.py stop
和:
python create_service.py remove
我得到确认
stopping service
和
Service removed
在控制台中。但是在状态下的任务管理器中,它说:停止挂起。
此外,websocket 仍在响应客户端。所以,显然它挂了。
最终我可以使用 taskkill 将其杀死,但一定是出了什么问题。
我只能通过 websocket 看到这种行为。原始示例中的其他代码可以正常启动和停止。所以我猜它与 python 的 asyncio 有关。
有没有人有过这个问题的经验或更深入了解这里发生的事情?
基础class(service_base.py)的复制代码:
'''
SMWinservice
by Davide Mastromatteo
Base class to create winservice in Python
-----------------------------------------
Instructions:
1. Just create a new class that inherits from this base class
2. Define into the new class the variables
_svc_name_ = "nameOfWinservice"
_svc_display_name_ = "name of the Winservice that will be displayed in scm"
_svc_description_ = "description of the Winservice that will be displayed in scm"
3. Override the three main methods:
def start(self) : if you need to do something at the service initialization.
A good idea is to put here the inizialization of the running condition
def stop(self) : if you need to do something just before the service is stopped.
A good idea is to put here the invalidation of the running condition
def main(self) : your actual run loop. Just create a loop based on your running condition
4. Define the entry point of your module calling the method "parse_command_line" of the new class
5. Enjoy
'''
import socket
import win32serviceutil
import servicemanager
import win32event
import win32service
class SMWinservice(win32serviceutil.ServiceFramework):
'''Base class to create winservice in Python'''
_svc_name_ = 'pythonService'
_svc_display_name_ = 'Python Service'
_svc_description_ = 'Python Service Description'
@classmethod
def parse_command_line(cls):
'''
ClassMethod to parse the command line
'''
win32serviceutil.HandleCommandLine(cls)
def __init__(self, args):
'''
Constructor of the winservice
'''
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
'''
Called when the service is asked to stop
'''
self.stop()
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
'''
Called when the service is asked to start
'''
self.start()
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def start(self):
'''
Override to add logic before the start
eg. running condition
'''
pass
def stop(self):
'''
Override to add logic before the stop
eg. invalidating running condition
'''
pass
def main(self):
'''
Main class to be ovverridden to add logic
'''
pass
# entry point of the module: copy and paste into the new module
# ensuring you are calling the "parse_command_line" of the new created class
if __name__ == '__main__':
SMWinservice.parse_command_line()
我修改的服务创建者(create_service.py):
import time
import random
from pathlib import Path
from service_base import SMWinservice
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
class MyService(SMWinservice):
_svc_name_ = "_MyService"
_svc_display_name_ = "Winservice Example"
_svc_description_ = "Simple example for a service"
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
start_server = websockets.serve(hello, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
if __name__ == '__main__':
MyService.parse_command_line()
websocket 客户端 (client.py):
import asyncio
import websockets
async def hello():
uri = "ws://localhost:8765"
async with websockets.connect(uri) as websocket:
name = input("What's your name? ")
await websocket.send(name)
print(f"> {name}")
greeting = await websocket.recv()
print(f"< {greeting}")
asyncio.get_event_loop().run_until_complete(hello())
您对 stop()
方法的实现除了设置一个标志,表明您的其余代码不以任何方式进行检查外,没有做任何事情。
stop()
的实施实际上应该停止服务,在您的情况下是停止事件循环。这可以通过调用 loop.stop()
来完成,请小心使用线程安全的 API,因为 stop()
很可能是从不同的线程调用的。例如,您可以:
- 修改
main
赋值self.loop = asyncio.get_event_loop()
,
- 修改
stop
调用self.loop.call_soon_threadsafe(self.loop.stop)
.
我想 运行 一个 websocket 服务器作为 windows 服务。 我能够通过从 Python 角落复制和修改用于创建服务的代码来创建一个简单的回显服务器:
https://www.thepythoncorner.com/2018/08/how-to-create-a-windows-service-in-python/
我使用了这里的代码:
https://websockets.readthedocs.io/en/stable/intro.html
创建 websocket 回显服务器。
我可以安装服务:
python create_service.py install
启动服务:
python create_service.py start
套接字服务器运行没有问题。但是当我尝试像这样停止服务时:
python create_service.py stop
和:
python create_service.py remove
我得到确认
stopping service
和
Service removed
在控制台中。但是在状态下的任务管理器中,它说:停止挂起。 此外,websocket 仍在响应客户端。所以,显然它挂了。
最终我可以使用 taskkill 将其杀死,但一定是出了什么问题。 我只能通过 websocket 看到这种行为。原始示例中的其他代码可以正常启动和停止。所以我猜它与 python 的 asyncio 有关。 有没有人有过这个问题的经验或更深入了解这里发生的事情?
基础class(service_base.py)的复制代码:
'''
SMWinservice
by Davide Mastromatteo
Base class to create winservice in Python
-----------------------------------------
Instructions:
1. Just create a new class that inherits from this base class
2. Define into the new class the variables
_svc_name_ = "nameOfWinservice"
_svc_display_name_ = "name of the Winservice that will be displayed in scm"
_svc_description_ = "description of the Winservice that will be displayed in scm"
3. Override the three main methods:
def start(self) : if you need to do something at the service initialization.
A good idea is to put here the inizialization of the running condition
def stop(self) : if you need to do something just before the service is stopped.
A good idea is to put here the invalidation of the running condition
def main(self) : your actual run loop. Just create a loop based on your running condition
4. Define the entry point of your module calling the method "parse_command_line" of the new class
5. Enjoy
'''
import socket
import win32serviceutil
import servicemanager
import win32event
import win32service
class SMWinservice(win32serviceutil.ServiceFramework):
'''Base class to create winservice in Python'''
_svc_name_ = 'pythonService'
_svc_display_name_ = 'Python Service'
_svc_description_ = 'Python Service Description'
@classmethod
def parse_command_line(cls):
'''
ClassMethod to parse the command line
'''
win32serviceutil.HandleCommandLine(cls)
def __init__(self, args):
'''
Constructor of the winservice
'''
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
'''
Called when the service is asked to stop
'''
self.stop()
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
'''
Called when the service is asked to start
'''
self.start()
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
self.main()
def start(self):
'''
Override to add logic before the start
eg. running condition
'''
pass
def stop(self):
'''
Override to add logic before the stop
eg. invalidating running condition
'''
pass
def main(self):
'''
Main class to be ovverridden to add logic
'''
pass
# entry point of the module: copy and paste into the new module
# ensuring you are calling the "parse_command_line" of the new created class
if __name__ == '__main__':
SMWinservice.parse_command_line()
我修改的服务创建者(create_service.py):
import time
import random
from pathlib import Path
from service_base import SMWinservice
import asyncio
import websockets
async def hello(websocket, path):
name = await websocket.recv()
print(f"< {name}")
greeting = f"Hello {name}!"
await websocket.send(greeting)
print(f"> {greeting}")
class MyService(SMWinservice):
_svc_name_ = "_MyService"
_svc_display_name_ = "Winservice Example"
_svc_description_ = "Simple example for a service"
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def main(self):
start_server = websockets.serve(hello, "localhost", 8765)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
if __name__ == '__main__':
MyService.parse_command_line()
websocket 客户端 (client.py):
import asyncio
import websockets
async def hello():
uri = "ws://localhost:8765"
async with websockets.connect(uri) as websocket:
name = input("What's your name? ")
await websocket.send(name)
print(f"> {name}")
greeting = await websocket.recv()
print(f"< {greeting}")
asyncio.get_event_loop().run_until_complete(hello())
您对 stop()
方法的实现除了设置一个标志,表明您的其余代码不以任何方式进行检查外,没有做任何事情。
stop()
的实施实际上应该停止服务,在您的情况下是停止事件循环。这可以通过调用 loop.stop()
来完成,请小心使用线程安全的 API,因为 stop()
很可能是从不同的线程调用的。例如,您可以:
- 修改
main
赋值self.loop = asyncio.get_event_loop()
, - 修改
stop
调用self.loop.call_soon_threadsafe(self.loop.stop)
.