Skip to content

Latest commit

 

History

History
52 lines (43 loc) · 1.13 KB

README.md

File metadata and controls

52 lines (43 loc) · 1.13 KB

asygpy

asyncio signal handlers for Windows and Unix.

Reveiving Signals

import asygpy
import asyncio
import signal

async def main() -> None:
    notifier = asygpy.create_notifier()
    channel = (
        notifier.open_channel()
        .add_signal(signal.SIGINT)
        .add_signal(signal.SIGTERM)
    )
    notifier.start_notifying()

    signum = await channel.receive()
    if signum == signal.SIGINT:
        print("Shutting down due to SIGINT")
    elif signum == signal.SIGTERM:
        print("Shutting down due to SIGTERM")

    notifier.stop_notifying()

asyncio.run(main())

Receiving Multiple Signals

import asygpy
import asyncio
import signal

async def main() -> None:
    notifier = asygpy.create_notifier()
    channel = (
        notifier.open_channel()
        .add_signal(signal.SIGINT)
        .add_signal(signal.SIGTERM)
    )
    notifier.start_notifying()

    async for signum in channel:
        if signum == signal.SIGINT:
            print("I received SIGINT but I don't care!")
        elif signum == signal.SIGTERM:
            print("I received SIGTERM but I don't care!")

asyncio.run(main())