Skip to main content

Making services easy

Project description

Service Wrapper

A python tool to easily create services running under the os's service management [source code]

Function as a service

Make a script into a service running under Windows's scm (linux WIP) using a simple decorator.

Intended to work with PyInstaller (or alternatives) to create an executable that can be registered under scm.

python main.py still works. Function will block until KeyboardInterrupt is received.

main.py

from service_wrapper 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
  • startup should be non-blocking (open threads/processes).
  • For blocking functions look at Blocking Functions

Blocking functions


It is recommended to use a Generator as the decorated function but not required. In order to decorate a blocking function:

from service_wrapper import as_service
from service_wrapper.ext import BlockingService


@as_service(
    SERVICE_NAME,
    SERVICE_DISPLAY_NAME,
    SERVICE_ENTRYPOINT_COMMAND,
    base=BlockingService,
)
def main():
    run_logic()
    
if __name__ == "__main__":
    main()

NOTES:

  • When invoked using scm (PyInstalled and installed as a service), BlockingService will run main() is a separate spawned process.
  • SIGINT will be sent to that process the scm stop is called

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 import get_service
from service_wrapper.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()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

service_wrapper-0.0.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

service_wrapper-0.0.2-cp312-cp312-win_amd64.whl (7.2 kB view details)

Uploaded CPython 3.12Windows x86-64

File details

Details for the file service_wrapper-0.0.2.tar.gz.

File metadata

  • Download URL: service_wrapper-0.0.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.2 CPython/3.12.7 Windows/11

File hashes

Hashes for service_wrapper-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5adedf39da95f122f058d4657bc77ff94b617b55aadd395dd6b0d8de3b09b806
MD5 7601d9b5e0c17047b6ade81e5b347ef5
BLAKE2b-256 b1c62d80ed4666fff3eb450d55b6796cfe539eaf435f7b619fe3c31ea7f06c6a

See more details on using hashes here.

File details

Details for the file service_wrapper-0.0.2-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for service_wrapper-0.0.2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 b7caa02d6f1866d0a37332de3d2cde39619ef28a2e1cdba8b30f5f8f8a31eb1c
MD5 a44d9e78e1b91c53395099d4316e22f5
BLAKE2b-256 2df668a9e8e88ac9f38aa9685f2cc52c22bac7e9d584b2c4e58f32e83fd2c9d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page