Skip to main content

Objects and patterns for working with processes in Python using the multiprocessing library

Project description

https://img.shields.io/travis/borgstrom/offspring.svg https://img.shields.io/codecov/c/github/borgstrom/offspring.svg Latest PyPI version

This is a collection of objects and patterns for working with processes in Python using the multiprocessing library.

The main idea is that you express your unit of work as a simple method on an object and then when that object is instantiated the work will be run in a subprocess.

Use cases

Offspring was built to address the following use cases for running code in a subprocess.

Run something once

from offspring import Subprocess


class MyTask(Subprocess):
    def __init__(self, arg1):
        # this is run in the parent process and is used to prepare your object
        self.arg = arg
        self.start()

    def run(self):
        # this will be run in the child process and completes your work
        # ...


MyTask('this is arg1').wait()

Run in a loop

from offspring import SubprocessLoop


class MyTask(SubprocessLoop):
    def __init__(self, arg1):
        # this is run in the parent process and is used to prepare your object
        self.arg = arg
        self.start()

    def begin(self):
        # called at the start of the loop in your child process

    def loop(self):
        # called each loop iteration in your your child process
        # it should return a sleep duration until the next loop, or False to stop the loop

    def end(self):
        # called at the end of the loop in your child process


MyTask('this is arg1').wait()

Implementation details

.process

Each Subprocess object has a .process attribute that is the multiprocessing.Process object.

.start

Creates the subprocess. If you do not customize your __init__ method then you do not need to explicitly call .start. If you do remember to call .start at the end of it.

.wait

If you need to wait for your child process you can call .wait on your Subprocess object. This is just a shortcut to .join on the multiprocessing.Process object.

WAIT_FOR_CHILD

Defaults to False.

If set to True on your Subprocess class then a Pipe will be used to block the parent process until the child has started. This is useful when you want to ensure that your Subprocess object is started and .run is called even if the parent process exits quickly.

class MyTask(Subprocess):
    WAIT_FOR_CHILD = True

    def run(self):
        print("This will always print")

MyTask()

The SubprocessLoop class does this to ensure that your object has begin & end called (loop may not be called as a TERM signal received during startup will prevent the loop from every actually completing other than begin & end).

TERMINATE_ON_SHUTDOWN

Defaults to True.

If set to False then when .shutdown is called on a Subprocess object the child process will not be terminated before being joined. This means that the parent will block until the child completes the .run function.

import time

class MyTask(Subprocess):
    TERMINATE_ON_SHUTDOWN = False

    def run(self):
        time.sleep(2)

MyTask()

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

offspring-0.0.3.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

offspring-0.0.3-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file offspring-0.0.3.tar.gz.

File metadata

  • Download URL: offspring-0.0.3.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for offspring-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2c52646129e4750cfc9909de9b3545935995eff6a70bedac7e7cb89a81a6f89d
MD5 20d86bfc2be69bfb421accd8e2aa3a20
BLAKE2b-256 9ed7043f4bc270ea0930c4ee69f3b63c88111b53af9492116fb77b87e3a8fd79

See more details on using hashes here.

File details

Details for the file offspring-0.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for offspring-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fdf65e47b36d874dd7364548a077b7f943ce6203d3699c8153c6b39223f77ab5
MD5 9941b8567fba687bb5f9a72637538ee2
BLAKE2b-256 158e8fd980a7744110d1971bd0ff3024292eca91408c8048905e2bc2d6d1b2d7

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