MCPcopy
hub / github.com/tornadoweb/tornado / fork_processes

Function fork_processes

tornado/process.py:83–176  ·  view source on GitHub ↗

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

(
    num_processes: Optional[int], max_restarts: Optional[int] = None
)

Source from the content-addressed store, hash-verified

81
82
83def fork_processes(
84 num_processes: Optional[int], max_restarts: Optional[int] = None
85) -> int:
86 """Starts multiple worker processes.
87
88 If ``num_processes`` is None or <= 0, we detect the number of cores
89 available on this machine and fork that number of child
90 processes. If ``num_processes`` is given and > 0, we fork that
91 specific number of sub-processes.
92
93 Since we use processes and not threads, there is no shared memory
94 between any server code.
95
96 Note that multiple processes are not compatible with the autoreload
97 module (or the ``autoreload=True`` option to `tornado.web.Application`
98 which defaults to True when ``debug=True``).
99 When using multiple processes, no IOLoops can be created or
100 referenced until after the call to ``fork_processes``.
101
102 In each child process, ``fork_processes`` returns its *task id*, a
103 number between 0 and ``num_processes``. Processes that exit
104 abnormally (due to a signal or non-zero exit status) are restarted
105 with the same id (up to ``max_restarts`` times). In the parent
106 process, ``fork_processes`` calls ``sys.exit(0)`` after all child
107 processes have exited normally.
108
109 max_restarts defaults to 100.
110
111 Availability: Unix
112 """
113 if sys.platform == "win32":
114 # The exact form of this condition matters to mypy; it understands
115 # if but not assert in this context.
116 raise Exception("fork not available on windows")
117 if max_restarts is None:
118 max_restarts = 100
119
120 global _task_id
121 assert _task_id is None
122 if num_processes is None or num_processes <= 0:
123 num_processes = cpu_count()
124 gen_log.info("Starting %d processes", num_processes)
125 children = {}
126
127 def start_child(i: int) -> Optional[int]:
128 pid = os.fork()
129 if pid == 0:
130 # child process
131 _reseed_random()
132 global _task_id
133 _task_id = i
134 return i
135 else:
136 children[pid] = i
137 return None
138
139 for i in range(num_processes):
140 id = start_child(i)

Callers 1

test_multi_processMethod · 0.90

Calls 3

cpu_countFunction · 0.85
start_childFunction · 0.85
waitMethod · 0.45

Tested by 1

test_multi_processMethod · 0.72