tornado.process — Utilities for multiple processes¶
Utilities for working with multiple processes, including both forkingthe server into multiple processes and managing subprocesses.
- exception
tornado.process.
CalledProcessError
[源代码]¶
An alias forsubprocess.CalledProcessError
.
tornado.process.
fork_processes
(_num_processes, max_restarts=100)[源代码]¶
Starts multiple worker processes.
Ifnumprocesses
is None or <= 0, we detect the number of coresavailable on this machine and fork that number of childprocesses. Ifnum_processes
is given and > 0, we fork thatspecific number of sub-processes.
Since we use processes and not threads, there is no shared memorybetween any server code.
Note that multiple processes are not compatible with the autoreloadmodule (or theautoreload=True
option totornado.web.Application
which defaults to True whendebug=True
).When using multiple processes, no IOLoops can be created orreferenced until after the call tofork_processes
.
In each child process,fork_processes
returns its _task id, anumber between 0 andnumprocesses
. Processes that exitabnormally (due to a signal or non-zero exit status) are restartedwith the same id (up tomax_restarts
times). In the parentprocess,fork_processes
returns None if all child processeshave exited normally, but will otherwise only exit by throwing anexception.
tornado.process.
task_id
()[源代码]¶
Returns the current task id, if any.
Returns None if this process was not created byfork_processes
.
- _class
tornado.process.
Subprocess
(*args, **kwargs)[源代码]¶
Wrapssubprocess.Popen
with IOStream support.
The constructor is the same assubprocess.Popen
with the followingadditions:
- stdin, stdout, and stderr may have the valuetornado.process.Subprocess.STREAM, which will make the correspondingattribute of the resulting Subprocess a PipeIOStream.
- A new keyword argument ioloop may be used to pass in an IOLoop.
TheSubprocess.STREAM
option and theset_exit_callback
andwait_for_exit
methods do not work on Windows. There istherefore no reason to use this class instead ofsubprocess.Popen
on that platform.
在 4.1 版更改: Theio_loop
argument is deprecated.set_exit_callback
(_callback)[源代码]¶
Runscallback
when this process exits.
The callback takes one argument, the return code of the process.
This method uses aSIGCHLD
handler, which is a global settingand may conflict if you have other libraries trying to handle thesame signal. If you are using more than oneIOLoop
it maybe necessary to callSubprocess.initialize
first to designateoneIOLoop
to run the signal handlers.
In many cases a close callback on the stdout or stderr streamscan be used as an alternative to an exit callback if thesignal handler is causing a problem.
waitfor_exit
(_raise_error=True)[源代码]¶
Returns aFuture
which resolves when the process exits.
Usage:- ret = yield proc.waitfor_exit()
This is a coroutine-friendly alternative toset_exit_callback
(and a replacement for the blockingsubprocess.Popen.wait
).
By default, raisessubprocess.CalledProcessError
if the processhas a non-zero exit status. Usewait_for_exit(raise_error=False)
to suppress this behavior and return the exit status without raising.
4.2 新版功能.- ret = yield proc.waitfor_exit()
- _classmethod
initialize
(io_loop=None)[源代码]¶
Initializes theSIGCHLD
handler.
The signal handler is run on anIOLoop
to avoid locking issues.Note that theIOLoop
used for signal handling need not be thesame one used by individual Subprocess objects (as long as theIOLoops
are each running in separate threads).
在 4.1 版更改: Theioloop
argument is deprecated.
原文:
https://tornado-zh-cn.readthedocs.io/zh_CN/latest/process.html