• tornado源码分析-多进程


    1.源码文件
    process.py

    2.fork子进程

    def fork_processes(num_processes, max_restarts=100):
    	...
    	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
    	...
        while children:
            try:
                pid, status = os.wait()
            except OSError as e:
                ...
            if pid not in children:
                continue
            id = children.pop(pid)
            ...
    	sys.exit(0)
    

    num_processes:指定子进程的数量,不指定为CPU处理器的数目
    max_restarts:启动失败,重启次数
    start_child:fork子进程,将子进程id放入children集合
    while children:等待所有子进程完成
    sys.exit(0):结束父进程

    3.子进程

    class Subprocess(object):
    	@classmethod
        def initialize(cls, io_loop=None):
    		...
    	def set_exit_callback(self, callback):
    		...
    	def wait_for_exit(self, raise_error=True):
    		...
    

    Subprocess是对subprocess.Popen的包装,支持IO输入输出
    subprocess.Popen是在一个新进程中执行一个子程序

    initialize:
    指定子进程发送信号对应的ioloop

    set_exit_callback:
    当子进程结束时,执行回调
    callback有一个参数,子进程返回码
    如果有多个IOLoop,需要指定发送SIGCHLD信号对应的IOLoop

    wait_for_exit:
    同set_exit_callback,区别在于wait_for_exit是异步的

  • 相关阅读:
    用mysql++读写二进制
    mysql++的release版本当机的问题
    mongo的安装
    什么是新装用电和增加用电
    填写用电报装申请表
    湖南省居民生活阶梯电价政策问答
    什么是阶梯电价
    用case when 动态给sql 添加 查询条件
    js 原型 继承
    可编辑的表格
  • 原文地址:https://www.cnblogs.com/shijingjing07/p/7903878.html
Copyright © 2020-2023  润新知