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


logging.basicConfig(level=logging.DEBUG)

def run():
    print(
        "Proceess #{id} has PID: {pid}".format(
            id=forklib.get_id(),
            pid=os.getpid()
        )
    )
    sleep(1)


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



if __name__ == '__main__':
    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

async_callback example

import asyncio
import forklib
import logging
import os
from time import sleep


logging.basicConfig(level=logging.DEBUG)

def run():
    print(
        "Proceess #{id} has PID: {pid}".format(
            id=forklib.get_id(),
            pid=os.getpid()
        )
    )
    sleep(1)

async def amain():
    await asyncio.sleep(0.5)
    print("Async callback finished")


def main():
    print("Master proccess has PID: {0}".format(os.getpid()))

    forklib.fork(
        4, run,
        async_callback=amain,
        # Wait all incomplete async tasks, otherwise cancel (default)
        wait_async_callback = True,
    )



if __name__ == '__main__':
    main()

thread_callback example

import logging
import os

from time import sleep

import forklib


logging.basicConfig(level=logging.DEBUG)

def run():
    print(
        "Proceess #{id} has PID: {pid}".format(
            id=forklib.get_id(),
            pid=os.getpid()
        )
    )
    sleep(1)

def thread_callback():
    sleep(0.5)
    print("Thread callback finished")


def main():
    print("Master proccess has PID: {0}".format(os.getpid()))

    forklib.fork(
        4, run,
        thread_callback=thread_callback,
        # Wait theread_callback, otherwise exit (default)
        wait_thread_callback = True,
    )



if __name__ == '__main__':
    main()

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 of the copy-on-write mechanism will be used.

from forklib import fork_map
import logging


logging.basicConfig(level=logging.INFO)


def map_func(item):
    return item + 1


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


if __name__ == '__main__':
    main()

Versioning

This software follows Semantic Versioning

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

forklib-0.4.2.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

forklib-0.4.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file forklib-0.4.2.tar.gz.

File metadata

  • Download URL: forklib-0.4.2.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for forklib-0.4.2.tar.gz
Algorithm Hash digest
SHA256 f980905c54aa4c13c6867e13238b97de2256f6d38daf1ac45217c27d6b3a8d58
MD5 e831409634d3666ceea559924b2d94c6
BLAKE2b-256 7c84f6b9561e47995507902edf97a3afed0fc210bc29cd12ae89e3d76e1c96ea

See more details on using hashes here.

File details

Details for the file forklib-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: forklib-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2

File hashes

Hashes for forklib-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e314440efa269fa2d82343e1e59d42ad2932aa718103c42b1b3e53f368bc8ec2
MD5 6ff606117963aba8a43d174a688c1d01
BLAKE2b-256 41506b8c18ef464902e9da3f6add43f6424a1435b1a528a96142329774f9ffff

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page