python - 为什么 pywin32 服务没有正确停止?

标签 python websocket python-asyncio pywin32

我想将 websocket 服务器作为 Windows 服务运行。 我能够通过从 Python 角复制和修改用于创建服务的代码来创建一个简单的 echo 服务器:

https://www.thepythoncorner.com/2018/08/how-to-create-a-windows-service-in-python/

我使用了这里的代码:

https://websockets.readthedocs.io/en/stable/intro.html

创建 websocket echo 服务器。

我可以安装该服务:

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有关。 有谁对这个问题有经验或者更深入地了解这里发生的事情吗?

复制的基类代码(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)

关于python - 为什么 pywin32 服务没有正确停止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58719744/

相关文章:

python - 如何使用 asyncio 复杂地管理 shell 进程?

python - Numpy 尽可能将字符串转换为 float

python - 如何确定通过 Python 运行的程序(最好是子进程)是否崩溃或成功结束

jquery - 自动将来自服务器的消息附加到 div 标签

javascript - 带有 proxytable 和 websockets 的 Node http 代理

python - 如何编写自定义事件循环策略?

python - OpenGL/通用图形的最佳动态语言

python - 如何使用python抓取aspx页面

Spring 5 响应式(Reactive) WebSocket : recommended use

python - Asyncio 使 HTTP 请求变慢?