Skip to main content

Efficient library for running Python code in isolated subprocesses.

Project description

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

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

subprocess_execution-0.1.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

subprocess_execution-0.1.1-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file subprocess_execution-0.1.1.tar.gz.

File metadata

  • Download URL: subprocess_execution-0.1.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.13

File hashes

Hashes for subprocess_execution-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9adb7e5ee2844d65b1c3b6b9d8e5c579a4f3f8c7013f46b9a66f5b1e5260b380
MD5 1eb3db65d9a244be15c0c47deae55ac8
BLAKE2b-256 57752b381fa4fb545ed2e1cd49b9b5e32aa1a393801f9b64b4da35d8a9039de2

See more details on using hashes here.

File details

Details for the file subprocess_execution-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for subprocess_execution-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 029cd0ea5c9fe5e2f08b6cb2b56cbadf0e392a936a95431f27c5593fca5571ce
MD5 358ed30f4d95b4ac7462b18668d09716
BLAKE2b-256 6178065e90a213fc03a86b6a1c1c6038030a763e57c911ba83f80b5e6302f21e

See more details on using hashes here.

Supported by

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