fiber.process
Fiber introduces a new concept called job-backed processes. It is similar to the process in Python's multiprocessing library, but more flexible: while a process in multiprocessing only runs on a local machine, a Fiber process can run remotely on a different machine or locally on the same machine.
When starting a new Fiber process, Fiber creates a new job with the proper Fiber backend on the current computer cluster. Fiber uses containers to encapsulate the running environment of current processes, including all the required files, input data, and other dependent program packages, etc., to ensure everything is self-contained. All the child processes are started with the same container image as the parent process to guarantee a consistent running environment.
Because each process is a cluster job, its life cycle is the same as any job on the cluster.
Process
Process(self, group=None, target=None, name=None, args=(), kwargs={}, *, daemon)
Create and manage Fiber processes.
The API is compatible with Python's multiprocessing.Process, check here for multiprocessing's documents.
Example usage:
p = Process(target=f, args=('Fiber',)) p.start()
pid¶
Process.pid
Return the current process PID. If the process hasn't been fully started,
the value will be None
. This PID is assigned by Fiber and is different
from the operating system process PID. The value of pid
is derived from
the job ID of the underlying job that runs this Fiber process.
name¶
Process.name
Return the name of this Fiber process. This value need to be set before the start of the Process.
Example:
p = fiber.Process(target=print, args=("demo")) p.name = "DemoProcess"
daemon¶
Process.daemon
In multiprocessing, if a process has daemon set, it will be terminated when it's parent process exits. In Fiber, current this value has no effect, all processes will be cleaned up when their parent exits.
authkey¶
Process.authkey
Authkey is used to authenticate between parent and child processes. It is a byte string.
exitcode¶
Process.exitcode
The exit code of current process. If the process has not exited, the
exitcode is None
. If the current process has exited, the value of this
is an integer.
sentinel¶
Process.sentinel
Returns a file descriptor that becomes "ready" when the process exits. You
can call select
and other eligible functions that works on fds on this
file descriptor.
run
Process.run()
Run the target function of current process (in current process)
Returns:
return value of the target function
start
Process.start()
Start this process.
Under the hood, Fiber calls the API on the computer cluster to start a new job and run the target function in the new job.
terminate
Process.terminate()
Terminate current process.
When running locally, Fiber sends an SIGTERM signal to the child process. When running on a computer cluster, Fiber calls the corresponding API on that platform to terminate the job that runs Fiber process.
join
Process.join(timeout=None)
Wait for this process to terminate.
Arguments:
timeout
: The maximum duration of time in seconds that this call should wait before return. iftimeout
isNone
(default value), this method will wait forever until the process terminates. Iftimeout
is0
, it will check if the process has exited and return immediately.
Returns:
The exit code of this process
is_alive
Process.is_alive()
Check if current process is still alive
Returns:
True
if current process is still alive. Returns False
if
it's not alive.
active_children
active_children()
Get a list of children processes of the current process.
Returns:
A list of children processes.
Example:
p = fiber.Process(target=time.sleep, args=(10,)) p.start() print(fiber.active_children())
current_process
current_process()
Return a Process object representing the current process.
Example:
print(fiber.current_process())