Dirty worker process that loads dirty apps and handles requests. Each worker runs its own asyncio event loop and listens on a worker-specific Unix socket for requests from the DirtyArbiter.
| 94 | |
| 95 | |
| 96 | class DirtyWorker: |
| 97 | """ |
| 98 | Dirty worker process that loads dirty apps and handles requests. |
| 99 | |
| 100 | Each worker runs its own asyncio event loop and listens on a |
| 101 | worker-specific Unix socket for requests from the DirtyArbiter. |
| 102 | """ |
| 103 | |
| 104 | SIGNALS = [getattr(signal, "SIG%s" % x) for x in |
| 105 | "ABRT HUP QUIT INT TERM USR1".split()] |
| 106 | |
| 107 | def __init__(self, age, ppid, app_paths, cfg, log, socket_path): |
| 108 | """ |
| 109 | Initialize a dirty worker. |
| 110 | |
| 111 | Args: |
| 112 | age: Worker age (for identifying workers) |
| 113 | ppid: Parent process ID |
| 114 | app_paths: List of dirty app import paths |
| 115 | cfg: Gunicorn config |
| 116 | log: Logger |
| 117 | socket_path: Path to this worker's Unix socket |
| 118 | """ |
| 119 | self.age = age |
| 120 | self.pid = "[booting]" |
| 121 | self.ppid = ppid |
| 122 | self.app_paths = app_paths |
| 123 | self.cfg = cfg |
| 124 | self.log = log |
| 125 | self.socket_path = socket_path |
| 126 | self.booted = False |
| 127 | self.aborted = False |
| 128 | self.alive = True |
| 129 | self.tmp = WorkerTmp(cfg) |
| 130 | self.apps = {} |
| 131 | self._server = None |
| 132 | self._loop = None |
| 133 | self._executor = None |
| 134 | |
| 135 | def __str__(self): |
| 136 | return f"<DirtyWorker {self.pid}>" |
| 137 | |
| 138 | def notify(self): |
| 139 | """Update heartbeat timestamp.""" |
| 140 | self.tmp.notify() |
| 141 | |
| 142 | def init_process(self): |
| 143 | """ |
| 144 | Initialize the worker process after fork. |
| 145 | |
| 146 | This is called in the child process after fork. It sets up |
| 147 | the environment, loads apps, and starts the main run loop. |
| 148 | """ |
| 149 | # Set environment variables |
| 150 | if self.cfg.env: |
| 151 | for k, v in self.cfg.env.items(): |
| 152 | os.environ[k] = v |
| 153 |
no outgoing calls