Skip to main content

Execute a function in a Python subprocess instead of using the Python main process.

Project description

subprocessed

Execute a function in a subprocess instead of using the main process.

It allows you to protect your main process from ugly things done in the functionalities you are using:

  • Global variables definition.
  • sys.path modification.
  • Segmentation fault and other wild crashes.
  • Static C/C++ variables.
  • Fortran 77 COMMON.
  • Etc.

Install

Install subprocessed from pip:

pip install subprocessed

Usage

Decorate your function with subprocessed

After installation, you can use the subprocessed decorator as follow:

from subprocessed import subprocessed

@subprocessed
def do_ugly_things(a: int, b: str, c: float):
    """Function hiding ugly thing done by the ``ugly`` module."""
    import ugly # Hide also your imports!
    return ugly.do_horrible_things(parameters)

# Call serenely your module
result1, output1 = do_ugly_things(1, "dummy", c=3.7)
result2, output2 = do_ugly_things(2, "meh", c=100.5)

Get what's happening in the decorated function seamlessly

Everything happening in the subprocessed function is transmitted to the main process:

  • Returned values.
  • Raised Exceptions.
  • Send warnings.
  • Proper exit with zero exit code.

Check if the subprocess is alive... or not

By default, the subprocess status is automatically checked every second. You can control that with the check_delay parameter when decorating your function.

from subprocessed import subprocessed
@subprocessed(check_delay=0.1)  # Check the subprocess every 100 ms
def do_ugly_things():
    pass

If the subprocess has died, a multiprocessing.ProcessError is raised.

Deactivate the subprocess checking

You can also completely deactivate the verification with None:

from subprocessed import subprocessed
@subprocessed(check_delay=None)  # No checking
def do_ugly_things():
    pass

Remember when doing that: if the subprocess crashes, the execution of the main process will be blocked.

Known limitations

Serializable inputs and outputs

Transmitting objects between processes is possible only if they are serializable.

Daemon child processes cannot create children processes

An exception will be raised if you try to execute a subprocessed function in a daemon child process.

Only compatible with POSIX systems

This forks Python process so it is not possible to use it on Windows and MacOS.

Project details


Release history Release notifications | RSS feed

This version

0.0

Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

subprocessed-0.0-2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file subprocessed-0.0-2-py3-none-any.whl.

File metadata

File hashes

Hashes for subprocessed-0.0-2-py3-none-any.whl
Algorithm Hash digest
SHA256 1fe82e96bc84048284fc74fb0f22d648b6422b27058a4284593a9055b27e5cb6
MD5 be5800996a379e75d39188aa80f03877
BLAKE2b-256 e13d96542dce9d6f9195a668044a6d9bf22d86a86372347db298ae1bca6a5a50

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