Service Wrapper
A python tool to easily create services running under the os's service management [source code]
Function as a service
Using a simple decorator, make your script a service running under:
- Windows's scm
- linux's systemd (needs additional testing)
Intended to work with PyInstaller (or alternatives) to create an executable that can be registered under scm or systemd.
python main.py still works. Function will block until KeyboardInterrupt is received.
main.py
from service_wrapper.windows import as_service
@as_service(SERVICE_NAME, SERVICE_DISPLAY_NAME, SERVICE_ENTRYPOINT_COMMAND)
def main():
startup()
try:
yield
finally:
cleanup()
if __name__ == "__main__":
main()
NOTES:
- The decorated function should not accept arguments
startupshould be non-blocking (open threads/processes)- In linux function may be blocking
- For blocking functions. look at Blocking Functions
Blocking functions
It is recommended to use a Generator as the decorated function but not required. in linux, a blocking function will behave normally. In order to decorate a blocking function in windows:
from service_wrapper.windows import as_service
from service_wrapper.windows import BlockingService
@as_service(
SERVICE_NAME,
SERVICE_DISPLAY_NAME,
SERVICE_ENTRYPOINT_COMMAND,
svc_class=BlockingService,
)
def main():
run_logic()
if __name__ == "__main__":
main()
NOTES:
- When invoked using scm (PyInstalled and installed as a service),
BlockingServicewill runmain()is a separate spawned process. - SIGINT will be sent to that process the scm stop is called
svc_classattribute is not supported in linux
Service Tooling
Controls for installing\removing the service are provided using the ServiceTools.
They are to be used externally in scripts to streamline installation\removal etc.
For example, usage in CI with a tool like invoke
from pathlib import Path
from invoke import Context, task
from service_wrapper.windows import get_service
from service_wrapper.windows.service_tools import ServiceTools
service_tools = ServiceTools(get_service(main))
EXECUTABLE_PATH = Path("./dist/svc.exe")
@task()
def build(context: Context) -> None:
context.run(
f"pyinstaller "
f"--onefile "
f"--name=svc "
f"main.py"
)
@task(build)
def install(_: Context) -> None:
service_tools.install_service(EXECUTABLE_PATH)
@task
def restart(_: Context) -> None:
service_tools.stop_service()
service_tools.start_service()
Release files for service_wrapper 0.0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| service_wrapper-0.0.3.tar.gz | 7.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| service_wrapper-0.0.3-cp312-cp312-win_amd64.whl | CPython 3.12 | CPython 3.12 | Windows x86-64 | Details |
Total release size: 16.5 kB
Release files / service_wrapper-0.0.3.tar.gz
| Download URL | service_wrapper-0.0.3.tar.gz |
|---|---|
| Size | 7.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a0c1986cd74d3e471733a2e2cef8fa4f6aec30212f7c1489c9c83cedd8dc2bfc
|
|
BLAKE2b-256 checksum How to use checksums |
5f34497aae99c250d5668da607879a8f0068e72a4fa077069ab977e671614d14
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.22.2 CPython/3.12.7 Windows/11
|
Release files / service_wrapper-0.0.3-cp312-cp312-win_amd64.whl
| Download URL | service_wrapper-0.0.3-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 9.2 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
28efff6ae7b0f37668272b82e51c1880a585902b92b53aa797130e8f43445999
|
|
BLAKE2b-256 checksum How to use checksums |
87d2571a74b462bb79fea34b862fbfe8e38aa73215fe811b559a21c0c9bd1626
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.22.2 CPython/3.12.7 Windows/11
|