Skip to content

Commit

Permalink
Merge pull request benoitc#3080 from odyfatouros/Fix-#3079-worker_cla…
Browse files Browse the repository at this point in the history
…ss-parameter-accepts-class

Fix for issue benoitc#3079, worker_class parameter accepts a class
  • Loading branch information
benoitc authored Aug 7, 2024
2 parents 52234e7 + 08364f0 commit ad7c1de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 8 additions & 6 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,20 @@ def parser(self):
def worker_class_str(self):
uri = self.settings['worker_class'].get()

# are we using a threaded worker?
is_sync = uri.endswith('SyncWorker') or uri == 'sync'
if is_sync and self.threads > 1:
return "gthread"
return uri
if isinstance(uri, str):
# are we using a threaded worker?
is_sync = uri.endswith('SyncWorker') or uri == 'sync'
if is_sync and self.threads > 1:
return "gthread"
return uri
return uri.__name__

@property
def worker_class(self):
uri = self.settings['worker_class'].get()

# are we using a threaded worker?
is_sync = uri.endswith('SyncWorker') or uri == 'sync'
is_sync = isinstance(uri, str) and (uri.endswith('SyncWorker') or uri == 'sync')
if is_sync and self.threads > 1:
uri = "gunicorn.workers.gthread.ThreadWorker"

Expand Down
17 changes: 17 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from gunicorn.app.base import Application
from gunicorn.app.wsgiapp import WSGIApplication
from gunicorn.errors import ConfigError
from gunicorn.util import load_class
from gunicorn.workers.sync import SyncWorker
from gunicorn import glogging
from gunicorn.instrument import statsd
Expand Down Expand Up @@ -55,6 +56,10 @@ def load(self):
pass


class CustomWorker(SyncWorker):
pass


class WSGIApp(WSGIApplication):
def __init__(self):
super().__init__("no_usage", prog="gunicorn_test")
Expand All @@ -63,6 +68,18 @@ def load(self):
pass


def test_worker_class():

c = config.Config()
c.set("worker_class", CustomWorker)
assert c.worker_class == CustomWorker

try:
assert isinstance(load_class(c.worker_class), object)
except AttributeError:
pytest.fail("'load_class doesn't support type class argument'")


def test_defaults():
c = config.Config()
for s in config.KNOWN_SETTINGS:
Expand Down

0 comments on commit ad7c1de

Please sign in to comment.