Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Example of fork in Python relevant to playground.

Views: 90
Image: ubuntu2004-dev
Kernel: SageMath 9.4
greet = 'hello world'
@fork def f(): global greet replacement = "k" greet = greet.replace('l', replacement) print(greet) f()
hekko workd
print(greet)
hello world
print(replacement)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-5-c5b11e7025c5> in <module> ----> 1 print(replacement) NameError: name 'replacement' is not defined
help(fork)
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:
f = open("test.txt",'w') f.write('foo') f.close()
f = open('test.txt')
@fork def playground(): global f print(f.read()) f.close() playground()
foo
# f exists and is not closed, but the forked process got the same # file handle and already read everything. print(f.read())