Почему служба pywin32 не останавливается правильно? - PullRequest
0 голосов
/ 06 ноября 2019

Я хочу запустить сервер веб-сокетов в качестве службы 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, но что-то должно быть не так. Я могу видеть это поведение только с веб-сокетом. Другой код, как в оригинальном примере, запускается и останавливается просто отлично. Так что я предполагаю, что это связано с асинхронностью питона. Кто-нибудь имеет опыт решения этой проблемы или глубже изнутри относительно того, что здесь происходит?

Скопированный код для базового класса (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()

Клиент веб-сокета (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())

1 Ответ

1 голос
/ 06 ноября 2019

Ваша реализация метода stop() не делает ничего, кроме установки флага, который остальная часть вашего кода не проверяет никаким образом.

Реализация stop() должна фактически остановитьсервис, в вашем случае, остановив цикл обработки событий. Это может быть достигнуто с помощью вызова loop.stop(), при этом следует осторожно использовать потокобезопасный API, поскольку stop(), вероятно, вызывается из другого потока. Например, вы можете:

  • изменить main для назначения self.loop = asyncio.get_event_loop(), а
  • изменить stop для вызова self.loop.call_soon_threadsafe(self.loop.stop).
...