标签:error man use 热更新 time 否则 mon rip zed
Arbiter(self).run()
def run()
self.init_signal()
self.LISTENERS = create_sockets(self.cfg, self.log)
self.manage_workers()
while True:
if no signal in SIG_QUEUE
self.sleep()
else:
handle_signal()
Arbiter.spawn_worker1 # prefork.py 2 import sys 3 import socket 4 import select 5 import os 6 import time 7 8 def do_sub_process(): 9 pid = os.fork() 10 if pid < 0: 11 print ‘fork error‘ 12 sys.exit(-1) 13 elif pid > 0: 14 print ‘fork sub process %d‘ % pid 15 return 16 17 # must be child process 18 time.sleep(1) 19 print ‘sub process will exit‘, os.getpid(), os.getppid() 20 sys.exit(0) 21 22 def main(): 23 sub_num = 2 24 for i in range(sub_num): 25 do_sub_process() 26 time.sleep(10) 27 print ‘main process will exit‘, os.getpid() 28 29 if __name__ == ‘__main__‘: 30 main()
1 def kill_worker(self, pid, sig): 2 """ 3 Kill a worker 4 5 :attr pid: int, worker pid 6 :attr sig: `signal.SIG*` value 7 """ 8 try: 9 os.kill(pid, sig) 10 except OSError as e: 11 if e.errno == errno.ESRCH: 12 try: 13 worker = self.WORKERS.pop(pid) 14 worker.tmp.close() 15 self.cfg.worker_exit(self, worker) 16 return 17 except (KeyError, OSError): 18 return 19 raise
def sleep(self):
""" Sleep until PIPE is readable or we timeout.
A readable PIPE means a signal occurred.
"""
ready = select.select([self.PIPE[0]], [], [], 1.0) # self.PIPE = os.pipe()
if not ready[0]:
return
while os.read(self.PIPE[0], 1):
pass
代码里面的注释写得非常清楚,要么PIPE可读立即返回,要么等待超时。管道可读是因为有信号发生。这里看看pipe函数
os.pipe()Create a pipe. Return a pair of file descriptors (r,w) usable for reading and writing, respectively.
def wakeup(self):
"""
Wake up the arbiter by writing to the PIPE
"""
os.write(self.PIPE[1], b‘.‘)
最后附上Arbiter的信号处理:
--preloadoption), Gunicorn will also load the new version.标签:error man use 热更新 time 否则 mon rip zed
原文地址:http://www.cnblogs.com/yezuhui/p/6855662.html