标签:ble ber load cat 更新 ast should flush html
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()
1 def spawn_worker(self): 2 self.worker_age += 1 3 worker = self.worker_class(self.worker_age, self.pid, self.LISTENERS, 4 self.app, self.timeout / 2.0, 5 self.cfg, self.log) 6 self.cfg.pre_fork(self, worker) 7 pid = os.fork() 8 if pid != 0: 9 self.WORKERS[pid] = worker 10 return pid 11 12 # Process Child 13 worker_pid = os.getpid() 14 try: 15 util._setproctitle("worker [%s]" % self.proc_name) 16 self.log.info("Booting worker with pid: %s", worker_pid) 17 self.cfg.post_fork(self, worker) 18 worker.init_process() 19 sys.exit(0) 20 except SystemExit: 21 raise 22 except AppImportError as e: 23 self.log.debug("Exception while loading the application", 24 exc_info=True) 25 print("%s" % e, file=sys.stderr) 26 sys.stderr.flush() 27 sys.exit(self.APP_LOAD_ERROR) 28 except: 29 self.log.exception("Exception in worker process"), 30 if not worker.booted: 31 sys.exit(self.WORKER_BOOT_ERROR) 32 sys.exit(-1) 33 finally: 34 self.log.info("Worker exiting (pid: %s)", worker_pid) 35 try: 36 worker.tmp.close() 37 self.cfg.worker_exit(self, worker) 38 except: 39 self.log.warning("Exception during worker exit:\n%s", 40 traceback.format_exc())
1 # 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的信号处理:
--preload
option), Gunicorn will also load the new version.标签:ble ber load cat 更新 ast should flush html
原文地址:http://www.cnblogs.com/xybaby/p/6297089.html