Skip to main content

Offspring

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.
        # It receives whatever arguments were supplied to the constructor.
        self.arg1 = arg1

    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 the same as init for Subprocess.
        self.arg1 = arg1

    def begin(self):
        # Called before the start of the loop in your child process.
        # ...

    def loop(self):
        # Called each loop iteration in your your child process.
        # It can return a sleep duration until the next loop, or False to stop the loop.
        # ...

    def end(self):
        # Called after the end of the loop, before termination in your child process.
        # ...


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

Implementation details

.init(*args, **kwargs)

Called when an instance of your class is created. It receives the same arguments as the __init__ method, so you are encouraged to explicitly define the arguments you expect.

.start()

Creates the subprocess. This is automatically called unless you set EXPLICIT_START to True.

.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.

.shutdown()

This will send a TERM signal to the child process, unless TERMINATE_ON_SHUTDOWN is False, and then calls .wait() to join the child process. It is automatically called whenever the parent process exits via the atexit module.

.process

Each Subprocess object has a .process attribute that is 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()

EXPLICIT_START

Defaults to False.

If set to True then when you instantiate an object you must explicitly call .start() before the child process will be spawned.

class MyTask(Subprocess):
    EXPLICIT_START = True

    def run(self):
        print("Running!")


task = MyTask()
# Do some other work
task.start()
# Running! is now printed

Metadata

Release files for offspring 0.1.1

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

Source distribution (sdist)

Source distribution for offspring 0.1.1
File Size Uploaded
offspring-0.1.1.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for offspring 0.1.1
File Interpreter ABI Platform
offspring-0.1.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 10.9 kB

Release files / offspring-0.1.1.tar.gz

Download URL offspring-0.1.1.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
e30c37b5f98c5bc2e0889a0c24a3ced91fac10f3bff090873d3c14ea7acc0c53
BLAKE2b-256 checksum
How to use checksums
08511e7130113a14882969b78081ee11bc159946b70fbb098bd839903239f613
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / offspring-0.1.1-py2.py3-none-any.whl

Download URL offspring-0.1.1-py2.py3-none-any.whl
Size 6.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
3fe701aa67a982e899cfefaecdbec29c2223fd93779aaa60e9dd3e74d24ac6d0
BLAKE2b-256 checksum
How to use checksums
61d3cd60771acf94c9171521288005784812692a729cd76a82ddcf714e0fcbe8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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