Async Fork

This module implements a simple os.fork()-like interface, but in an asynchronous way with full support for PID file descriptors on Python 3.9 or higher and the Linux kernel 5.4 or higher.

It internally synchronizes the beginning and readiness status of child processes so that the users may assume that the child process is completely interruptible after afork() returns.

class AbstractChildProcess[source]

The abstract interface to control and monitor a forked child process.

abstract send_signal(signum: int) None[source]

Send a UNIX signal to the child process. If the child process is already terminated, it will log a warning message and return.

abstract async wait() int[source]

Wait until the child process terminates or reclaim the child process’ exit code if already terminated. If there are other coroutines that has waited the same process, it may return 255 and log a warning message.

PosixChildProcess(pid: int) None[source]

A POSIX-compatible version of AbstractChildProcess.

PidfdChildProcess(pid: int, pidfd: int) None[source]

A PID file descriptor-based version of AbstractChildProcess.

async afork(child_func: Callable[[], int]) aiotools.fork.AbstractChildProcess[source]

Fork the current process and execute the given function in the child. The return value of the function will become the exit code of the child process.

Parameters

child_func – A function that represents the main function of the child and returns an integer as its exit code. Note that the function must set up a new event loop if it wants to run asyncio codes.