Skip to main content

Subprocess Execution

Efficient tool for running Python code in isolated subprocesses.

PyPI Version Build Status License

Table of Contents


Overview

A simple and efficient way to execute arbitrary Python functions in subprocesses, while seamlessly relaying runtime information back to the main process. It is especially useful for diagnosing and resolving system issues that might otherwise crash the Python interpreter.

Designed with minimal dependencies and an intuitive interface, this tool makes it easy to execute any Python callable.

Installation

From PyPI

Install the library using pip:

pip install subprocess-execution

From Source

Clone the repository and install:

git clone https://github.com/joshwadd/subprocess-execution.git
cd subprocess-execution
pip install .

Usage

Importing the Decorator

First, import the run_in_subprocess decorator from the module:

from subprocess_execution import run_in_subprocess

Basic Usage

Apply the @run_in_subprocess decorator to any function you want to execute in a subprocess:

@run_in_subprocess()
def my_function(arg1, arg2):
    # Function logic here
    return arg1 + arg2

Call the function as usual:

result = my_function(5, 10)
print(result)  # Output: 15

Using a Timeout

You can specify a timeout (in seconds) after which the subprocess will be terminated if the function hasn't completed:

@run_in_subprocess(timeout_seconds=300)  # Timeout after 5 minutes
def long_running_function():
    # Potentially long-running operations
    ...

Handling Exceptions

If an exception occurs within the subprocess, it will be raised in the parent process:

@run_in_subprocess()
def faulty_function():
    raise ValueError("An error occurred")

try:
    faulty_function()
except Exception as e:
    print(f"Caught an exception: {e}")

Example

from your_module_name import run_in_subprocess

@run_in_subprocess(timeout_seconds=10)
def compute_sum(numbers):
    total = 0
    for num in numbers:
        total += num
    return total

if __name__ == "__main__":
    numbers = list(range(1000000))
    try:
        result = compute_sum(numbers)
        print(f"The sum is: {result}")
    except Exception as e:
        print(f"An error occurred: {e}")

License

This project is licensed under the MIT License.

Contact

For questions or feedback, feel free to reach out:

Email: josh.waddington1@gmail.com

Metadata

Release files for subprocess-execution 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 subprocess-execution 0.1.1
File Size Uploaded
subprocess_execution-0.1.1.tar.gz 9.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for subprocess-execution 0.1.1
File Interpreter ABI Platform
subprocess_execution-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / subprocess_execution-0.1.1.tar.gz

Download URL subprocess_execution-0.1.1.tar.gz
Size 9.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9adb7e5ee2844d65b1c3b6b9d8e5c579a4f3f8c7013f46b9a66f5b1e5260b380
BLAKE2b-256 checksum
How to use checksums
57752b381fa4fb545ed2e1cd49b9b5e32aa1a393801f9b64b4da35d8a9039de2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.13

Release files / subprocess_execution-0.1.1-py3-none-any.whl

Download URL subprocess_execution-0.1.1-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
029cd0ea5c9fe5e2f08b6cb2b56cbadf0e392a936a95431f27c5593fca5571ce
BLAKE2b-256 checksum
How to use checksums
6178065e90a213fc03a86b6a1c1c6038030a763e57c911ba83f80b5e6302f21e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.13

Release history Release notifications | RSS feed

This release

0.1.1 This release

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