Skip to main content

A context manager to fork.

Project description

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

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

forking-0.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

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

forking-0.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file forking-0.2.tar.gz.

File metadata

  • Download URL: forking-0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for forking-0.2.tar.gz
Algorithm Hash digest
SHA256 f6206e32c78e23507ba7f1cbe9c4341f8c32ab9d410cc169ea5b6ac1c33f5ced
MD5 2072c2af8b639e13bf196cdaabda36cb
BLAKE2b-256 f13dbee5e65c61646640dff07340badf7d094c7687571473066f50613bce4846

See more details on using hashes here.

File details

Details for the file forking-0.2-py3-none-any.whl.

File metadata

  • Download URL: forking-0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for forking-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 21bc736a48da103c4e336bb8073e6bb82a974e9a25abd38af54223133251528b
MD5 7b9c3fd70d24dffe0dc46aa83b47b604
BLAKE2b-256 2f83571990fd313414b2cd5c5745cf8c5481cd5180ff32fc1942e5cbbf7c3165

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