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 subprocessed
from pip
:
pip install 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)
Everything happening in the subprocessed function is transmitted to the main process:
- Returned values.
- Raised Exceptions.
- Send warnings.
- Proper exit with zero exit code.
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.
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.
Transmitting objects between processes is possible only if they are serializable.
An exception will be raised if you try to execute a subprocessed function in a daemon child process.
This forks Python process so it is not possible to use it on Windows and MacOS.