### Navigation
- [index](# "General Index")
- [modules](# "Python Module Index") |
- [next](# "Integration with other services") |
- [previous](# "tornado.queues – Queues for coroutines") |
- [Tornado 4.4.dev1 documentation](#) »
- [Coroutines and concurrency](#) »
# `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`[[source]](#)
An alias for [`subprocess.CalledProcessError`](https://docs.python.org/3.4/library/subprocess.html#subprocess.CalledProcessError "(in Python v3.4)") [https://docs.python.org/3.4/library/subprocess.html#subprocess.CalledProcessError].
`tornado.process.``cpu_count`()[[source]](#)
Returns the number of processors on this machine.
`tornado.process.``fork_processes`(*num_processes*, *max_restarts=100*)[[source]](#)
Starts multiple worker processes.
If `num_processes` is None or <= 0, we detect the number of coresavailable on this machine and fork that number of childprocesses. If `num_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 the `autoreload=True` option to [`tornado.web.Application`](# "tornado.web.Application")which defaults to True when `debug=True`).When using multiple processes, no IOLoops can be created orreferenced until after the call to `fork_processes`.
In each child process, `fork_processes` returns its *task id*, anumber between 0 and `num_processes`. Processes that exitabnormally (due to a signal or non-zero exit status) are restartedwith the same id (up to `max_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`()[[source]](#)
Returns the current task id, if any.
Returns None if this process was not created by [`fork_processes`](# "tornado.process.fork_processes").
*class *`tornado.process.``Subprocess`(**args*, ***kwargs*)[[source]](#)
Wraps `subprocess.Popen` with IOStream support.
The constructor is the same as `subprocess.Popen` with the followingadditions:
- `stdin`, `stdout`, and `stderr` may have the value`tornado.process.Subprocess.STREAM`, which will make the correspondingattribute of the resulting Subprocess a [`PipeIOStream`](# "tornado.iostream.PipeIOStream").
- A new keyword argument `io_loop` may be used to pass in an IOLoop.
Changed in version 4.1: The `io_loop` argument is deprecated.
`set_exit_callback`(*callback*)[[source]](#)
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 settingand may conflict if you have other libraries trying to handle thesame signal. If you are using more than one `IOLoop` it maybe necessary to call [`Subprocess.initialize`](# "tornado.process.Subprocess.initialize") first to designateone `IOLoop` 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.
`wait_for_exit`(*raise_error=True*)[[source]](#)
Returns a [`Future`](# "tornado.concurrent.Future") which resolves when the process exits.
Usage:
~~~
ret = yield proc.wait_for_exit()
~~~
This is a coroutine-friendly alternative to [`set_exit_callback`](# "tornado.process.Subprocess.set_exit_callback")(and a replacement for the blocking [`subprocess.Popen.wait`](https://docs.python.org/3.4/library/subprocess.html#subprocess.Popen.wait "(in Python v3.4)") [https://docs.python.org/3.4/library/subprocess.html#subprocess.Popen.wait]).
By default, raises [`subprocess.CalledProcessError`](https://docs.python.org/3.4/library/subprocess.html#subprocess.CalledProcessError "(in Python v3.4)") [https://docs.python.org/3.4/library/subprocess.html#subprocess.CalledProcessError] if the processhas a non-zero exit status. Use `wait_for_exit(raise_error=False)`to suppress this behavior and return the exit status without raising.
New in version 4.2.
*classmethod *`initialize`(*io_loop=None*)[[source]](#)
Initializes the `SIGCHLD` handler.
The signal handler is run on an [`IOLoop`](# "tornado.ioloop.IOLoop") to avoid locking issues.Note that the [`IOLoop`](# "tornado.ioloop.IOLoop") used for signal handling need not be thesame one used by individual Subprocess objects (as long as the`IOLoops` are each running in separate threads).
Changed in version 4.1: The `io_loop` argument is deprecated.
*classmethod *`uninitialize`()[[source]](#)
Removes the `SIGCHLD` handler.
© Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.
- User's guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example - a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- Web framework
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- HTTP servers and clients
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- Asynchronous networking
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream-based TCP server
- Coroutines and concurrency
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- Integration with other services
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- Utilities
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- Frequently Asked Questions
- Release notes
- What's new in Tornado 4.3
- What's new in Tornado 4.2.1
- What's new in Tornado 4.2
- What's new in Tornado 4.1
- What's new in Tornado 4.0.2
- What's new in Tornado 4.0.1
- What's new in Tornado 4.0
- What's new in Tornado 3.2.2
- What's new in Tornado 3.2.1
- What's new in Tornado 3.2
- What's new in Tornado 3.1.1
- What's new in Tornado 3.1
- What's new in Tornado 3.0.2
- What's new in Tornado 3.0.1
- What's new in Tornado 3.0
- What's new in Tornado 2.4.1
- What's new in Tornado 2.4
- What's new in Tornado 2.3
- What's new in Tornado 2.2.1
- What's new in Tornado 2.2
- What's new in Tornado 2.1.1
- What's new in Tornado 2.1
- What's new in Tornado 2.0
- What's new in Tornado 1.2.1
- What's new in Tornado 1.2
- What's new in Tornado 1.1.1
- What's new in Tornado 1.1
- What's new in Tornado 1.0.1
- What's new in Tornado 1.0