当前位置:网站首页>Tornado multi process service
Tornado multi process service
2022-07-26 09:14:00 【Or turn around】
tornado Multi process start
In the foreword "tornado And hello world" I have briefly introduced tornado Basic use of .tornado By default, a single process is started , The starting mode is as follows :
if __name__ == "__main__":
application = tornado.web.Application([(r"/", MainHandler)])
# application.listen(8888)
server = HTTPServer(application)
server.listen(8888)
tornado.ioloop.IOLoop.instance().start()
If you want to start multiple processes , The following modifications can be made :
if __name__ == "__main__":
application = tornado.web.Application([(r"/", MainHandler)])
server = HTTPServer(application)
server.bind(8888)
server.start(2)
tornado.ioloop.IOLoop.instance().start()
Generated after startup 3 A process ( Watch out for closure. debug Pattern ).start The parameters in the function are described as follows :
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 > 1, we fork that
specific number of sub-processes.
intend , When the parameter is less than or equal to 0 when , According to the current machine cpu Number of cores to create sub processes , Greater than 1 Directly create a child process according to the specified parameters .
to glance at bind Function description :
def bind(self, port, address=None, family=socket.AF_UNSPEC, backlog=128,
reuse_port=False):
"""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 call `listen` as a shortcut to the
sequence of `bind` and `start` calls.
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_INET`
or `socket.AF_INET6` to restrict to IPv4 or IPv6 addresses, otherwise
both will be used if available.
The ``backlog`` argument has the same meaning as for
`socket.listen <socket.socket.listen>`. The ``reuse_port`` argument
has the same meaning as for `.bind_sockets`.
This method may be called multiple times prior to `start` to listen
on multiple ports or interfaces.
.. versionchanged:: 4.4
Added the ``reuse_port`` argument.
"""
in other words , When start The argument to the function is 1 when ,listen Function can replace bind and start.
Multi process startup principle
Analyze the startup code :
So let's create one HTTPServer example . And then in bind Function , Called bind_sockets Method to create sockets. And then start In the method , call process.fork_processes(num_processes) Method to create a child process , And will sockets adopt add_sockets Method to add to server In the example .
in other words , The essence of this code is as follows :
if __name__ == "__main__":
application = tornado.web.Application([(r"/", MainHandler)])
sockets = tornado.netutil.bind_sockets(8888)
tornado.process.fork_processes(2)
server = HTTPServer(application)
server.add_sockets(sockets)
tornado.ioloop.IOLoop.instance().start()
Continue analysis fork_process function , The main process is as follows :
def start_child(i):
pid = os.fork()
if pid == 0:
# child process
_reseed_random()
global _task_id
_task_id = i
return i
else:
children[pid] = i
return None
for i in range(num_processes):
id = start_child(i)
if id is not None:
return id
num_restarts = 0
while children:
try:
pid, status = os.wait()
except OSError as e:
if errno_from_exception(e) == errno.EINTR:
continue
raise
if pid not in children:
continue
id = children.pop(pid)
if os.WIFSIGNALED(status):
gen_log.warning("child %d (pid %d) killed by signal %d, restarting",
id, pid, os.WTERMSIG(status))
elif os.WEXITSTATUS(status) != 0:
gen_log.warning("child %d (pid %d) exited with status %d, restarting",
id, pid, os.WEXITSTATUS(status))
else:
gen_log.info("child %d (pid %d) exited normally", id, pid)
continue
num_restarts += 1
if num_restarts > max_restarts:
raise RuntimeError("Too many child restarts, giving up")
new_id = start_child(id)
if new_id is not None:
return new_id
# All child processes exited cleanly, so exit the master process
# instead of just returning to right after the call to
# fork_processes (which will probably just start up another IOLoop
# unless the caller checks the return value).
sys.exit(0)
From this source code, we can see , The start of the subprocess is actually called linux The system provides fork function . When a child process exists , The parent process passes through a while Loop to process the termination signal of the child process , Try to restart the child process that has been stopped , Until the default maximum number of restarts is exceeded 100.
边栏推荐
- 838. Heap sorting
- Node-v download and application, ES6 module import and export
- ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
- ES6 modular import and export) (realize page nesting)
- 2B和2C
- at、crontab
- 原根与NTT 五千字详解
- Store a group of positive and negative numbers respectively, and count the number of 0 -- assembly language implementation
- tornado之多进程服务
- 优秀的 Verilog/FPGA开源项目介绍(三十零)- 暴力破解MD5
猜你喜欢

优秀的 Verilog/FPGA开源项目介绍(三十零)- 暴力破解MD5

公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链

at、crontab

CF1481C Fence Painting

(1) CTS tradefed test framework environment construction

Study notes of dataX

Study notes of automatic control principle -- correction and synthesis of automatic control system

Datax的学习笔记

Database operation skills 7

jvm命令归纳
随机推荐
PHP 之 Apple生成和验证令牌
服务器内存故障预测居然可以这样做!
ONTAP 9文件系统的限制
redis原理和使用-安装和分布式配置
Flask project learning (I) -- sayhello
839. Simulation reactor
JVM command induction
Database operation skills 7
Unity topdown character movement control
Database operation topic 1
Polynomial open root
The child and binary tree- open root inversion of polynomials
【final关键字的使用】
ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
NTT (fast number theory transformation) polynomial inverse 1500 word analysis
Summary of common activation functions for deep learning
CF1481C Fence Painting
聪明的美食家 C语言
Pat grade a a1013 battle over cities
JS - DataTables control on the number of displays per page