Pyjo.IOLoop.Subprocess - Subprocesses¶
from Pyjo.IOLoop.Subprocess import Subprocess
import time
# Operation that would block the event loop for 5 seconds
subprocess = Subprocess()
def blocking(subprocess):
time.sleep(5)
return '♥', 'Pyjoyment'
def done(subprocess, err, *results):
if err:
print(f'Subprocess error: {err}')
return
print(f'I {results[0]} {results[1]}!')
subprocess.run(blocking, done)
# Or import the module, as in Google Python Style Guide
import Pyjo.IOLoop.Subprocess
import time
# Operation that would block the event loop for 5 seconds
subprocess = Pyjo.IOLoop.Subprocess.new()
def blocking(subprocess):
time.sleep(5)
return '♥', 'Pyjoyment'
def done(subprocess, err, *results):
if err:
print(f'Subprocess error: {err}')
return
print(f'I {results[0]} {results[1]}!')
subprocess.run(blocking, done)
# Operation that would block the event loop for 5 seconds (with promise)
subprocess.run_p(blocking).then(lambda *results: print(f'I {results[0]} {results[1]}!')).catch(
lambda err: print(f'Subprocess error: {err}')
)
# Start event loop if necessary
if not subprocess.ioloop.is_running:
subprocess.ioloop.start()
Pyjo.IOLoop.Subprocess allows Pyjo.IOLoop to perform
computationally expensive operations in subprocesses, without blocking the
event loop.
The subprocess is a fork of the process, made with os.fork(), so the
first callback of run() can be any function,
a closure too, and it has the state of the parent. It runs where
os.fork() exists, not on Windows.
Events¶
Pyjo.IOLoop.Subprocess inherits all events from Pyjo.EventEmitter
and can emit the following new ones.
cleanup¶
subprocess.on('cleanup', lambda subprocess: ...)
Emitted in the subprocess right before the process will exit.
subprocess.on('cleanup', lambda subprocess: print(f'Process {os.getpid()} is about to exit'))
progress¶
subprocess.on('progress', lambda subprocess, *data: ...)
Emitted in the parent process when the subprocess calls the
progress() method.
spawn¶
subprocess.on('spawn', lambda subprocess: ...)
Emitted in the parent process when the subprocess has been spawned.
subprocess.on('spawn', lambda subprocess: print(f'Performing work in process {subprocess.pid}'))
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojo::IOLoop::Subprocess of Mojolicious 9.49.
Classes¶
- class Pyjo.IOLoop.Subprocess.Pyjo_IOLoop_Subprocess(*, deserialize: Callable[[bytes], Any] | None = None, ioloop: Pyjo_IOLoop | None = None, serialize: Callable[[Any], bytes] | None = None)[source]¶
Pyjo.IOLoop.Subprocessinherits all methods fromPyjo.EventEmitterand implements the following new ones.- deserialize: Callable[[bytes], Any]¶
cb = subprocess.deserialize subprocess.deserialize = lambda data: []
A callback used to deserialize subprocess return values, defaults to using
Pyjo.JSON.
- property exit_code: int | None[source]¶
code = subprocess.exit_code
Returns the subprocess exit code, or
Noneif the subprocess is still running.
- property ioloop: Pyjo_IOLoop[source]¶
loop = subprocess.ioloop subprocess.ioloop = Pyjo.IOLoop.new()
Event loop object to control, defaults to the global
Pyjo.IOLoopsingleton. Note that this attribute is weakened.
- property pid: int | None[source]¶
pid = subprocess.pid
Process id of the spawned subprocess if available.
- progress(*data: Anything) None[source]¶
subprocess.progress(*data)
Send data serialized with
Pyjo.JSONto the parent process at any time during the subprocess’s execution. Must be called by the subprocess and emits theprogressevent in the parent process with the data.# Send progress information to the parent process def blocking(subprocess): subprocess.progress('0%') time.sleep(5) subprocess.progress('50%') time.sleep(5) return 'Hello Pyjo!' def done(subprocess, err, *results): print('Progress is 100%') print(results[0]) subprocess.run(blocking, done) subprocess.on('progress', lambda subprocess, *data: print(f'Progress is {data[0]}'))
- run(child: Child, parent: Parent) Self[source]¶
subprocess = subprocess.run(lambda subprocess: ..., lambda subprocess, err, *results: ...)
Execute the first callback in a child process and wait for it to return a value, a tuple for several ones and
Nonefor none, without blockingioloopin the parent process. Then execute the second callback in the parent process with the error and the results. The return value of the first callback and the exception raised by it will be serialized withPyjo.JSON, so they can be shared between processes.
- run_p(child: Child) Pyjo_Promise[source]¶
promise = subprocess.run_p(lambda subprocess: ...)
Same as
run(), but returns aPyjo.Promiseobject instead of accepting a second callback.subprocess.run_p(blocking).then(lambda *results: print(f'I {results[0]} {results[1]}!')).catch( lambda err: print(f'Subprocess error: {err}') ).wait()