| Download
Example of fork in Python relevant to playground.
Project: Support and Testing
Views: 90Image: ubuntu2004-dev
Kernel: SageMath 9.4
In [1]:
In [3]:
hekko workd
In [4]:
hello world
In [5]:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-5-c5b11e7025c5> in <module>
----> 1 print(replacement)
NameError: name 'replacement' is not defined
In [6]:
Help on function fork in module sage.parallel.decorate:
fork(f=None, timeout=0, verbose=False)
Decorate a function so that when called it runs in a forked
subprocess. This means that it won't have any in-memory
side effects on the parent Sage process. The pexpect interfaces
are all reset.
INPUT:
- ``f`` -- a function
- ``timeout`` -- (default: 0) if positive, kill the subprocess after
this many seconds (wall time)
- ``verbose`` -- (default: ``False``) whether to print anything
about what the decorator does (e.g., killing the subprocess)
.. warning::
The forked subprocess will not have access to data created
in pexpect interfaces. This behavior with respect to pexpect
interfaces is very important to keep in mind when setting up
certain computations. It's the one big limitation of this
decorator.
EXAMPLES:
We create a function and run it with the ``fork`` decorator. Note
that it does not have a side effect. Despite trying to change
the global variable ``a`` below in ``g``, the variable ``a`` does not
get changed::
sage: a = 5
sage: @fork
....: def g(n, m):
....: global a
....: a = 10
....: return factorial(n).ndigits() + m
sage: g(5, m=5)
8
sage: a
5
We use ``fork`` to make sure that the function terminates after one
second, no matter what::
sage: @fork(timeout=1, verbose=True)
....: def g(n, m): return factorial(n).ndigits() + m
sage: g(5, m=5)
8
sage: g(10^7, m=5)
Killing subprocess ... with input ((10000000,), {'m': 5}) which took too long
'NO DATA (timed out)'
We illustrate that the state of the pexpect interface is not altered by
forked functions (they get their own new pexpect interfaces!)::
sage: gp.eval('a = 5')
'5'
sage: @fork()
....: def g():
....: gp.eval('a = 10')
....: return gp.eval('a')
sage: g()
'10'
sage: gp.eval('a')
'5'
We illustrate that the forked function has its own pexpect
interface::
sage: gp.eval('a = 15')
'15'
sage: @fork()
....: def g(): return gp.eval('a')
sage: g()
'a'
We illustrate that segfaulting subprocesses are no trouble at all::
sage: cython('def f(): print(<char*>0)')
sage: @fork
....: def g(): f()
sage: print("this works"); g()
this works...
<BLANKLINE>
------------------------------------------------------------------------
Unhandled SIG...
------------------------------------------------------------------------
'NO DATA'
/ext/sage/9.4/local/lib/python3.9/site-packages/sphinx/util/rst.py:55: DeprecationWarning: 'environmentfilter' is renamed to 'pass_environment', the old name will be removed in Jinja 3.1.
def heading(env: Environment, text: str, level: int = 1) -> str:
In [7]:
In [8]:
In [9]:
foo
In [10]:
See source code for fork decorator at https://github.com/sagemath/sage/blob/develop/src/sage/parallel/decorate.py#L418
In [0]: