| 685 | "mtype": "histogram"}) |
| 686 | |
| 687 | def spawn_worker(self): |
| 688 | self.worker_age += 1 |
| 689 | worker = self.worker_class(self.worker_age, self.pid, self.LISTENERS, |
| 690 | self.app, self.timeout / 2.0, |
| 691 | self.cfg, self.log) |
| 692 | self.cfg.pre_fork(self, worker) |
| 693 | |
| 694 | pid = os.fork() |
| 695 | if pid != 0: |
| 696 | worker.pid = pid |
| 697 | self.WORKERS[pid] = worker |
| 698 | self._stats['workers_spawned'] += 1 |
| 699 | return pid |
| 700 | |
| 701 | # Do not inherit the temporary files of other workers |
| 702 | for sibling in self.WORKERS.values(): |
| 703 | sibling.tmp.close() |
| 704 | |
| 705 | # Process Child |
| 706 | worker.pid = os.getpid() |
| 707 | try: |
| 708 | util._setproctitle("worker [%s]" % self.proc_name) |
| 709 | self.log.info("Booting worker with pid: %s", worker.pid) |
| 710 | if self.cfg.reuse_port: |
| 711 | worker.sockets = sock.create_sockets(self.cfg, self.log) |
| 712 | self.cfg.post_fork(self, worker) |
| 713 | worker.init_process() |
| 714 | sys.exit(0) |
| 715 | except SystemExit: |
| 716 | raise |
| 717 | except AppImportError as e: |
| 718 | self.log.debug("Exception while loading the application", |
| 719 | exc_info=True) |
| 720 | print("%s" % e, file=sys.stderr) |
| 721 | sys.stderr.flush() |
| 722 | sys.exit(self.APP_LOAD_ERROR) |
| 723 | except Exception as e: |
| 724 | self.log.exception("Exception in worker process") |
| 725 | print("%s" % e, file=sys.stderr) |
| 726 | sys.stderr.flush() |
| 727 | if not worker.booted: |
| 728 | sys.exit(self.WORKER_BOOT_ERROR) |
| 729 | sys.exit(-1) |
| 730 | finally: |
| 731 | self.log.info("Worker exiting (pid: %s)", worker.pid) |
| 732 | try: |
| 733 | worker.tmp.close() |
| 734 | self.cfg.worker_exit(self, worker) |
| 735 | except Exception: |
| 736 | self.log.warning("Exception during worker exit:\n%s", |
| 737 | traceback.format_exc()) |
| 738 | |
| 739 | def spawn_workers(self): |
| 740 | """\ |