Forking
Forking is a Python context manager that run the code in a different
process.
That means funny business:
>>> import os
>>> from forking import Forking
>>>
>>>
>>> with Forking():
... os._exit(0)
>>> print("I will survive!")
I will survive!
>>>
Obviously side effects are only executed in the child process so not visible to the parent:
>>> i = 1
>>> with Forking():
... i += 1
>>> print(i)
1
>>>
Capturing stdout and stderr
You can capture stdout and stderr though:
child = Forking()
with child:
print("Youpi")
print(child.stdout)
Note that we can't do with Forking() as child because the as child
part is not executed (as considered inside the with body).
Getting exit status informations
You can also get info about the exit status of your process though
child.exit which contains code, signal and has_core_dump:
>>> child = Forking()
>>> with child:
... os._exit(12)
>>> print(child.exit.code)
12
>>>
Reusing a context manager
A Forking context can be reused (but its process will not):
>>> child = Forking()
>>> with child:
... os._exit(21)
>>> print(child.exit.code)
21
>>> with child:
... os._exit(22)
>>> print(child.exit.code)
22
>>>
Timeout
You can specify a timeout for your child, the following snipet runs
in 0.2s, not 10s:
>>> import time
>>>
>>> child = Forking()
>>> child.timeout = 0.2
>>> with child:
... time.sleep(10)
...
>>>
When a child is killed after exceeding its deadline, you'll have a
SIGKILL in child.exit.signal:
>>> child.exit.signal
9
>>>
Custom child exception hooks
You can give a custom exception hook, either by subclassing:
class FriendlyForking(Forking):
"""A Forking class that use Friendly Traceback"""
def exception_hook(self, exc_type, exc_value, tb):
friendly_traceback.session.exception_hook(exc_type, exc_value, tb)
or if you're lazy:
forking = Forking()
forking.exception_hook = friendly_traceback.session.exception_hook
Metadata
Release files for Forking 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| forking-0.3.tar.gz | 5.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| forking-0.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.2 kB
Release files / forking-0.3.tar.gz
| Download URL | forking-0.3.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
abd58cf392b27160f7b6ac659aa80cb205da318766ec60b875b1ee731ffb708c
|
|
BLAKE2b-256 checksum How to use checksums |
3896deafe54f10ebe7ff9e4e484d05721be6e34b0479a647e35fe84f49f6fe1c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.12
|
Release files / forking-0.3-py3-none-any.whl
| Download URL | forking-0.3-py3-none-any.whl |
|---|---|
| Size | 4.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
23cfe4af8b1d7f9d53996e749c985ee5c14871444fdccb901a464d0605137e4d
|
|
BLAKE2b-256 checksum How to use checksums |
541f8dc5b89ec0fa5934ad5b8103dd68a70df50f95d90f2d5dc2e6f6756fd44f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.12
|