MCPcopy
hub / github.com/benoitc/gunicorn / _get_available_worker

Method _get_available_worker

gunicorn/dirty/arbiter.py:583–623  ·  view source on GitHub ↗

Get an available worker PID using round-robin selection. If app_path is provided, only returns workers that have loaded that specific app. Uses per-app round-robin to ensure fair distribution among eligible workers. Args: app_path: Optional impo

(self, app_path=None)

Source from the content-addressed store, hash-verified

581 await DirtyProtocol.write_message_async(client_writer, response)
582
583 async def _get_available_worker(self, app_path=None):
584 """
585 Get an available worker PID using round-robin selection.
586
587 If app_path is provided, only returns workers that have loaded
588 that specific app. Uses per-app round-robin to ensure fair
589 distribution among eligible workers.
590
591 Args:
592 app_path: Optional import path of the target app. If None,
593 returns any worker using global round-robin.
594
595 Returns:
596 Worker PID or None if no eligible workers are available.
597 """
598 # Determine eligible workers
599 if app_path and self.app_specs:
600 # Per-app allocation is configured - must return a worker
601 # that has this specific app
602 if app_path in self.app_worker_map:
603 eligible_pids = list(self.app_worker_map[app_path])
604 else:
605 # App not known or no workers have it
606 return None
607 else:
608 # No specific app requested, or no app specs configured
609 # (backward compatible) - any worker will do
610 eligible_pids = list(self.workers.keys())
611
612 if not eligible_pids:
613 return None
614
615 # Per-app round-robin for fairness
616 if app_path and self.app_specs:
617 idx = self._app_rr_indices.get(app_path, 0)
618 self._app_rr_indices[app_path] = (idx + 1) % len(eligible_pids)
619 else:
620 idx = self._worker_rr_index
621 self._worker_rr_index = (idx + 1) % len(eligible_pids)
622
623 return eligible_pids[idx % len(eligible_pids)]
624
625 async def _get_worker_connection(self, worker_pid):
626 """Get or create connection to a worker."""

Calls 2

keysMethod · 0.45
getMethod · 0.45