Skip to main content

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>

Metadata

Release files for pymultirun 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pymultirun 0.1.0
File Size Uploaded Provenance Actions
pymultirun-0.1.0.tar.gz 7.7 kB Without provenance Details for pymultirun-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pymultirun 0.1.0
File Interpreter ABI Platform Actions
pymultirun-0.1.0-py3-none-any.whl Python 3 none any Details for pymultirun-0.1.0-py3-none-any.whl

Total release size: 14.6 kB

Release files / pymultirun-0.1.0.tar.gz

Download URL pymultirun-0.1.0.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
58baf209045789bb53206f1c39e5f282ae7b4e1a44896bbac5d657fc61f81ed0
BLAKE2b-256 checksum
How to use checksums
9860946f7c851da8e809dd8da766c90ea029ad3afa8c9257140507ae67da7b26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Release files / pymultirun-0.1.0-py3-none-any.whl

Download URL pymultirun-0.1.0-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8422893d6a6f050065054c98da70fcd836b12e590b0164a53c9e8e38411840db
BLAKE2b-256 checksum
How to use checksums
99e63c69096d15541756dbbecb2fa1e73889efb2ffb969554fdc00d6a8385002
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.6

Security for pymultirun 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page