Giter Club home page Giter Club logo

windows-service's Introduction

windows-service

This is sample repo for setting up a Windows service from a python script. We've run into issues in the upgrade from python 3.7 to 3.10, and have found out painfully that the new asyncio default loop policy does not work when being used as a service.

Starting with python 3.8, the default loop policy has changed from SelectorEventLoop to ProactorEventLoop.

The issue is due to the ProactorEventLoop not supporting add_reader/writer. My guess is this is needed to properly support detaching from a process, because you can use the ProactorEventLoop to run the service in debug, which stays attached to the process.

Reference Links

Open Issues

Documentation

Getting started

After cloning this repo, install pywin32 and follow some post-installation steps.

Install pywin32

.\venv\Scripts\activate.ps1
python -m pip install -r .\requirements.txt

As an Administrator, you must run the post-install command to place the proper DLLs into the system python.

python .\venv\Scripts\pywin32_postinstall.py -install

Modify to run service properly from virtual environment

Copy the main.py script into the Scripts directory.

Copy-Item .\main.py -Destination .\venv\Scripts\

Copy the pythonservice.exe to the Scripts directory, next to the python script that will be run as the service.

Copy-Item .\venv\Lib\site-packages\win32\pythonservice.exe -Destination .\venv\Scripts\

Lastly, set the _exe_path_ class attribute to the pythonservice.exe placed inside Scripts. This is needed to ensure the virtual environment is used for the service.

_exe_path_ = str(Path(sys.exec_prefix).joinpath('Scripts', 'pythonservice.exe'))

Managing service

python .\venv\Scripts\main.py [ install | remove | start | debug ]

Code References

Looking at the pywin32 source code, when you run a service in debug, the service is started with os.system.

When you start the service without debug, it uses win32service.StartService(), which in turn calls a C function that requires a handle to the service. With no add_reader() method, I believe this is why it fails with the ProactorEventLoop.

windows-service's People

Contributors

kincerb avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.