Skip to content

Commit

Permalink
Replace select() usage with poll()
Browse files Browse the repository at this point in the history
  • Loading branch information
pchaseh committed Jun 8, 2023
1 parent add8a4c commit 4c7e6b0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 7 additions & 2 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import random
import select
import selectors
import signal
import sys
import time
Expand Down Expand Up @@ -76,6 +77,8 @@ def __init__(self, app):
0: sys.executable
}

self.poller = select.poll()

def _get_num_workers(self):
return self._num_workers

Expand Down Expand Up @@ -181,6 +184,8 @@ def init_signals(self):
util.set_non_blocking(p)
util.close_on_exec(p)

self.poller.register(self.PIPE[0], selectors.EVENT_READ)

self.log.close_on_exec()

# initialize all signals
Expand Down Expand Up @@ -357,8 +362,8 @@ def sleep(self):
A readable PIPE means a signal occurred.
"""
try:
ready = select.select([self.PIPE[0]], [], [], 1.0)
if not ready[0]:
events = self.poller.poll(1.0)
if len(events) < 1:
return
while os.read(self.PIPE[0], 1):
pass
Expand Down
6 changes: 6 additions & 0 deletions gunicorn/workers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import io
import os
import signal
import selectors
import sys
import time
import traceback
import select
from datetime import datetime
from random import randint
from ssl import SSLError
Expand Down Expand Up @@ -62,6 +64,7 @@ def __init__(self, age, ppid, sockets, app, timeout, cfg, log):
self.alive = True
self.log = log
self.tmp = WorkerTmp(cfg)
self.poller = select.poll()

def __str__(self):
return "<Worker %s>" % self.pid
Expand Down Expand Up @@ -113,6 +116,9 @@ def init_process(self):

self.wait_fds = self.sockets + [self.PIPE[0]]

for fd in self.wait_fds:
self.poller.register(fd, selectors.EVENT_READ)

self.log.close_on_exec()

self.init_signals()
Expand Down
8 changes: 4 additions & 4 deletions gunicorn/workers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ def accept(self, listener):
def wait(self, timeout):
try:
self.notify()
ret = select.select(self.wait_fds, [], [], timeout)
if ret[0]:
if self.PIPE[0] in ret[0]:
events = self.poller.poll(timeout)
if len(events) > 0:
if any(self.PIPE[0] == fd for fd, _ in events):
os.read(self.PIPE[0], 1)
return ret[0]
return events

except select.error as e:
if e.args[0] == errno.EINTR:
Expand Down

0 comments on commit 4c7e6b0

Please sign in to comment.