Kernel¶
Run an external program from async code without blocking the event loop. It is what runs CPU bound tasks in a subprocess, and it is available for running any command line tool from a task. See Run a Command Line Tool for a walkthrough.
fluid.utils.kernel.run
async
¶
run(
executable,
*args,
result_callback=None,
error_callback=None,
env=None,
stream_output=False,
stream_error=False,
terminate_grace_period=TERMINATE_GRACE_PERIOD,
input=None
)
Run an executable in a subprocess and wait for it to finish.
It returns the exit code of the process, negative when it was stopped by a signal. A non zero code is not raised as an error, check it.
The process is terminated when this coroutine is cancelled, which is what happens when the caller times out, so the process cannot outlive the code waiting for it.
| PARAMETER | DESCRIPTION |
|---|---|
executable
|
The program to run, a path or a name looked up in
TYPE:
|
*args
|
Arguments passed to the program, no shell is involved.
TYPE:
|
result_callback
|
Called with the bytes of stdout, discarded when not given.
TYPE:
|
error_callback
|
Called with the bytes of stderr, discarded when not given.
TYPE:
|
env
|
Environment of the process. It replaces the environment of the current process rather than extending it, pass
TYPE:
|
stream_output
|
Call
TYPE:
|
stream_error
|
The same as
TYPE:
|
terminate_grace_period
|
Seconds to wait after a SIGTERM before sending a SIGKILL, when the run is cancelled.
TYPE:
|
input
|
Bytes written to the stdin of the process, which is closed once they are written. When not given stdin is
TYPE:
|
Source code in fluid/utils/kernel.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
fluid.utils.kernel.run_python
async
¶
Run the current python interpreter in a subprocess.
A shortcut for run with sys.executable as the
executable, it accepts the same keyword arguments.
Source code in fluid/utils/kernel.py
fluid.utils.kernel.terminate
async
¶
terminate(process, grace_period=TERMINATE_GRACE_PERIOD)
Terminate a process unless it has already exited.
It sends a SIGTERM first and escalates to a SIGKILL when the process is still running after the grace period.
Source code in fluid/utils/kernel.py
fluid.utils.kernel.KernelCallback
module-attribute
¶
A callback receiving a chunk of the output of a process
fluid.utils.kernel.CollectBytes
¶
A callback collecting the output in data
fluid.utils.kernel.EncodedLog
¶
A callback decoding the output as utf-8 and passing it to a log function.
The decoded text is also kept in data.
Source code in fluid/utils/kernel.py
fluid.utils.kernel.Utf8Decoder
¶
Decode a stream of bytes chunks as utf-8.
A character split across two chunks is decoded once both have arrived, and invalid bytes are replaced rather than raising.
Source code in fluid/utils/kernel.py
fluid.utils.kernel.TERMINATE_GRACE_PERIOD
module-attribute
¶
Seconds to wait for a terminated process before killing it