Skip to main content

A multi-process runtime library with health checks

Project description

multirun

A multi-process runtime library with health checks, Excerpted from uvicorn.

If the script needs to use multiple processes and run for a long time, use it.


Installation

pip install pymultirun

Usage

Create a script that you want to run in multiple processes, in example.py:

import socket

from multirun import run_multiprocess
import asyncio

async def handle_echo(reader, writer):
    data = await reader.read(100)
    message = data.decode()
    addr = writer.get_extra_info('peername')

    print(f"Received {message!r} from {addr!r}")

    print(f"Send: {message!r}")
    writer.write(data)
    await writer.drain()

    print("Close the connection")
    writer.close()
    await writer.wait_closed()

async def main(sock: socket.socket):
    server = await asyncio.start_server(
        handle_echo, sock=sock)

    addrs = ', '.join(str(sock.getsockname()) for sock in server.sockets)
    print(f'Serving on {addrs}')

    async with server:
        await server.serve_forever()

def worker(sock: socket.socket) -> None:
    loop: asyncio.AbstractEventLoop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    try:
        loop.run_until_complete(main(sock))
    finally:
        loop.close()


if __name__ == '__main__':
    with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock:
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
        sock.bind(("::", 8888))
        run_multiprocess(worker, workers=2, args=(sock, ))

You can also use the command line, in example.py:

import os
import time

def worker_function(name: str, age:int, sleep_time=1, **kwargs):
    pid = os.getpid()
    count = 0
    print(f"Name {name}, Age {age}, Kwargs {kwargs}.")

    try:
        while True:
            time.sleep(sleep_time)
            count += 1
            print(f"Worker {name} (PID: {pid}) - count: {count}")
    except KeyboardInterrupt:
        print(f"Worker {name} (PID: {pid}) shutting down")

Run it:

multirun example:worker_function --workers 2 --timeout 3  --args vvanglro --args 23  --kwargs a 1 

Signal

You can use signals to stop the workers:

kill -SIGINT <pid>

Restart the workers:

kill -SIGHUP <pid>

Add workers:

kill -SIGTTIN <pid>

Reduce workers:

kill -SIGTTOU <pid>

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

pymultirun-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

pymultirun-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file pymultirun-0.1.0.tar.gz.

File metadata

  • Download URL: pymultirun-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.6

File hashes

Hashes for pymultirun-0.1.0.tar.gz
Algorithm Hash digest
SHA256 58baf209045789bb53206f1c39e5f282ae7b4e1a44896bbac5d657fc61f81ed0
MD5 bb88e29a0704ffcf501bc073d99205f6
BLAKE2b-256 9860946f7c851da8e809dd8da766c90ea029ad3afa8c9257140507ae67da7b26

See more details on using hashes here.

File details

Details for the file pymultirun-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pymultirun-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8422893d6a6f050065054c98da70fcd836b12e590b0164a53c9e8e38411840db
MD5 9cfb00292106d7ea25f5be7de242595d
BLAKE2b-256 99e63c69096d15541756dbbecb2fa1e73889efb2ffb969554fdc00d6a8385002

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