tornado.tcpserver — Basic IOStream-based TCP server¶
A non-blocking, single-threaded TCP server.
-
class
tornado.tcpserver.TCPServer(io_loop=None, ssl_options=None, max_buffer_size=None, read_chunk_size=None)[source]¶ A non-blocking, single-threaded TCP server.
To use
TCPServer, define a subclass which overrides thehandle_streammethod.To make this server serve SSL traffic, send the ssl_options dictionary argument with the arguments required for the
ssl.wrap_socketmethod, including “certfile” and “keyfile”:TCPServer(ssl_options={ "certfile": os.path.join(data_dir, "mydomain.crt"), "keyfile": os.path.join(data_dir, "mydomain.key"), })
TCPServerinitialization follows one of three patterns:listen: simple single-process:server = TCPServer() server.listen(8888) IOLoop.instance().start()
bind/start: simple multi-process:server = TCPServer() server.bind(8888) server.start(0) # Forks multiple sub-processes IOLoop.instance().start()
When using this interface, an
IOLoopmust not be passed to theTCPServerconstructor.startwill always start the server on the default singletonIOLoop.add_sockets: advanced multi-process:sockets = bind_sockets(8888) tornado.process.fork_processes(0) server = TCPServer() server.add_sockets(sockets) IOLoop.instance().start()
The
add_socketsinterface is more complicated, but it can be used withtornado.process.fork_processesto give you more flexibility in when the fork happens.add_socketscan also be used in single-process servers if you want to create your listening sockets in some way other thanbind_sockets.
New in version 3.1: The
max_buffer_sizeargument.-
listen(port, address='')[source]¶ Starts accepting connections on the given port.
This method may be called more than once to listen on multiple ports.
listentakes effect immediately; it is not necessary to callTCPServer.startafterwards. It is, however, necessary to start theIOLoop.
-
add_sockets(sockets)[source]¶ Makes this server start accepting connections on the given sockets.
The
socketsparameter is a list of socket objects such as those returned bybind_sockets.add_socketsis typically used in combination with that method andtornado.process.fork_processesto provide greater control over the initialization of a multi-process server.
-
add_socket(socket)[source]¶ Singular version of
add_sockets. Takes a single socket object.
-
bind(port, address=None, family=0, backlog=128)[source]¶ Binds this server to the given port on the given address.
To start the server, call
start. If you want to run this server in a single process, you can calllistenas a shortcut to the sequence ofbindandstartcalls.Address may be either an IP address or hostname. If it’s a hostname, the server will listen on all IP addresses associated with the name. Address may be an empty string or None to listen on all available interfaces. Family may be set to either
socket.AF_INETorsocket.AF_INET6to restrict to IPv4 or IPv6 addresses, otherwise both will be used if available.The
backlogargument has the same meaning as forsocket.listen.This method may be called multiple times prior to
startto listen on multiple ports or interfaces.
-
start(num_processes=1)[source]¶ Starts this server in the
IOLoop.By default, we run the server in this process and do not fork any additional child process.
If num_processes is
Noneor <= 0, we detect the number of cores available on this machine and fork that number of child processes. If num_processes is given and > 1, 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=Trueoption totornado.web.Applicationwhich defaults to True whendebug=True). When using multiple processes, no IOLoops can be created or referenced until after the call toTCPServer.start(n).