当前位置: 首页 > 文档资料 > Tornado 用户手册 >

tornado.process — Utilities for multiple processes

优质
小牛编辑
128浏览
2023-12-01

Utilities for working with multiple processes, including both forking the server into multiple processes and managing subprocesses.

exception tornado.process.CalledProcessError

An alias for .

tornado.process.cpu_count()

Returns the number of processors on this machine.

tornado.process.fork_processes(num_processes, max_restarts=100)

Starts multiple worker processes.

If num_processes is None or <= 0, we detect the number of cores available on this machine and fork that number of child processes. If num_processes is given and > 0, we fork that specific number of sub-processes.

Since we use processes and not threads, there is no shared memory between any server code.

Note that multiple processes are not compatible with the autoreload module (or the autoreload=True option to which defaults to True when debug=True). When using multiple processes, no IOLoops can be created or referenced until after the call to fork_processes.

In each child process, fork_processes returns its task id, a number between 0 and num_processes. Processes that exit abnormally (due to a signal or non-zero exit status) are restarted with the same id (up to max_restarts times). In the parent process, fork_processes returns None if all child processes have exited normally, but will otherwise only exit by throwing an exception.

tornado.process.task_id()

Returns the current task id, if any.

Returns None if this process was not created by .

class tornado.process.Subprocess(*args, **kwargs)

Wraps subprocess.Popen with IOStream support.

The constructor is the same as subprocess.Popen with the following additions:

  • stdin, stdout, and stderr may have the value tornado.process.Subprocess.STREAM, which will make the corresponding attribute of the resulting Subprocess a .
  • A new keyword argument io_loop may be used to pass in an IOLoop.

The Subprocess.STREAM option and the set_exit_callback and wait_for_exit methods do not work on Windows. There is therefore no reason to use this class instead of subprocess.Popen on that platform.

在 4.1 版更改: The io_loop argument is deprecated.

set_exit_callback(callback)

Runs callback when this process exits.

The callback takes one argument, the return code of the process.

This method uses a SIGCHLD handler, which is a global setting and may conflict if you have other libraries trying to handle the same signal. If you are using more than one IOLoop it may be necessary to call first to designate one IOLoop to run the signal handlers.

In many cases a close callback on the stdout or stderr streams can be used as an alternative to an exit callback if the signal handler is causing a problem.

wait_for_exit(raise_error=True)

Returns a which resolves when the process exits.

Usage:

ret = yield proc.wait_for_exit()

This is a coroutine-friendly alternative to (and a replacement for the blocking ).

By default, raises if the process has a non-zero exit status. Use wait_for_exit(raise_error=False) to suppress this behavior and return the exit status without raising.

4.2 新版功能.

classmethod initialize(io_loop=None)

Initializes the SIGCHLD handler.

The signal handler is run on an to avoid locking issues. Note that the used for signal handling need not be the same one used by individual Subprocess objects (as long as the IOLoops are each running in separate threads).

在 4.1 版更改: The io_loop argument is deprecated.

classmethod uninitialize()

Removes the SIGCHLD handler.