Skip to main content

Fork the single process easily

Project description

Fork the single process easily

Basic example

import forklib
import logging
import os
from time import sleep


def run():
        "Proceess #{id} has PID: {pid}".format(

def main():
    print("Master proccess has PID: {0}".format(os.getpid()))
    forklib.fork(4, run)

if __name__ == '__main__':

This code makes 4 forks. When you try to run it you will see something like this

Master proccess has PID: 39485
DEBUG:forklib.forking:Starting 4 processes
Proceess #1 has PID: 39487
Proceess #0 has PID: 39486
Proceess #2 has PID: 39488
Proceess #3 has PID: 39489
DEBUG:forklib.forking:Child with PID: 39487 Number: 1 exited normally
DEBUG:forklib.forking:Child with PID: 39489 Number: 3 exited normally
DEBUG:forklib.forking:Child with PID: 39488 Number: 2 exited normally
DEBUG:forklib.forking:Child with PID: 39486 Number: 0 exited normally

Forkme will be control forks. When subprocess will be killed or will exit with non-zero code it will be restarted immediately. e.g.:

Master proccess has PID: 7579
INFO:forklib:Starting 4 processes
Proceess #0 has PID: 7580
Proceess #1 has PID: 7581
Proceess #2 has PID: 7582
Proceess #3 has PID: 7583
WARNING:forklib:Child with PID: 7580 Number: 0 killed by signal 9, restarting
Proceess #0 has PID: 7584

Parallel iteration

You can load the large array of elements on the memory and process it in multiple processes. After forking the memory will not be copied, instead this the copy-on-write mechanism will be used.

from time import sleep

from forklib import fork_map, fork
import logging


def map_func(item):
    return item + 1

def main():
    for item in fork_map(map_func, range(20000), workers=10):

    fork(2, lambda: sleep(1), auto_restart=True)

if __name__ == '__main__':

Project details

Download files

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

Files for forklib, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size forklib-0.1.0-py2-none-any.whl (4.2 kB) File type Wheel Python version 2.7 Upload date Hashes View
Filename, size forklib-0.1.0.tar.gz (3.8 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page