Merge pull request #3080 from odyfatouros/Fix-#3079-worker_class-parameter-accepts-class

Fix for issue #3079, worker_class parameter accepts a class
This commit is contained in:
Benoit Chesneau 2024-08-07 08:47:20 +02:00 committed by GitHub
commit ad7c1de132
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 6 deletions

View File

@ -103,18 +103,20 @@ class Config(object):
def worker_class_str(self): def worker_class_str(self):
uri = self.settings['worker_class'].get() uri = self.settings['worker_class'].get()
# are we using a threaded worker? if isinstance(uri, str):
is_sync = uri.endswith('SyncWorker') or uri == 'sync' # are we using a threaded worker?
if is_sync and self.threads > 1: is_sync = uri.endswith('SyncWorker') or uri == 'sync'
return "gthread" if is_sync and self.threads > 1:
return uri return "gthread"
return uri
return uri.__name__
@property @property
def worker_class(self): def worker_class(self):
uri = self.settings['worker_class'].get() uri = self.settings['worker_class'].get()
# are we using a threaded worker? # 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: if is_sync and self.threads > 1:
uri = "gunicorn.workers.gthread.ThreadWorker" uri = "gunicorn.workers.gthread.ThreadWorker"

View File

@ -13,6 +13,7 @@ from gunicorn import config
from gunicorn.app.base import Application from gunicorn.app.base import Application
from gunicorn.app.wsgiapp import WSGIApplication from gunicorn.app.wsgiapp import WSGIApplication
from gunicorn.errors import ConfigError from gunicorn.errors import ConfigError
from gunicorn.util import load_class
from gunicorn.workers.sync import SyncWorker from gunicorn.workers.sync import SyncWorker
from gunicorn import glogging from gunicorn import glogging
from gunicorn.instrument import statsd from gunicorn.instrument import statsd
@ -55,6 +56,10 @@ class NoConfigApp(Application):
pass pass
class CustomWorker(SyncWorker):
pass
class WSGIApp(WSGIApplication): class WSGIApp(WSGIApplication):
def __init__(self): def __init__(self):
super().__init__("no_usage", prog="gunicorn_test") super().__init__("no_usage", prog="gunicorn_test")
@ -63,6 +68,18 @@ class WSGIApp(WSGIApplication):
pass 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(): def test_defaults():
c = config.Config() c = config.Config()
for s in config.KNOWN_SETTINGS: for s in config.KNOWN_SETTINGS: