mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Added "validate_class" option validator.
Now you able to use existed type instance or factory method to create and customize class during in-code configuration.
E.g.:
>>> settings.set('worker_class', MyWorkerClass)
>>> settings.set('worker_class', lambda: MyWorkerClass)
All other formats still valid.
This commit is contained in:
parent
70f470e63e
commit
16eb657584
@ -212,6 +212,13 @@ def validate_string(val):
|
|||||||
raise TypeError("Not a string: %s" % val)
|
raise TypeError("Not a string: %s" % val)
|
||||||
return val.strip()
|
return val.strip()
|
||||||
|
|
||||||
|
def validate_class(val):
|
||||||
|
if inspect.isfunction(val) or inspect.ismethod(val):
|
||||||
|
val = val()
|
||||||
|
if inspect.isclass(val):
|
||||||
|
return val
|
||||||
|
return validate_string(val)
|
||||||
|
|
||||||
def validate_callable(arity):
|
def validate_callable(arity):
|
||||||
def _validate_callable(val):
|
def _validate_callable(val):
|
||||||
if not callable(val):
|
if not callable(val):
|
||||||
@ -338,7 +345,7 @@ class WorkerClass(Setting):
|
|||||||
section = "Worker Processes"
|
section = "Worker Processes"
|
||||||
cli = ["-k", "--worker-class"]
|
cli = ["-k", "--worker-class"]
|
||||||
meta = "STRING"
|
meta = "STRING"
|
||||||
validator = validate_string
|
validator = validate_class
|
||||||
default = "sync"
|
default = "sync"
|
||||||
desc = """\
|
desc = """\
|
||||||
The type of workers to use.
|
The type of workers to use.
|
||||||
@ -740,7 +747,7 @@ class LoggerClass(Setting):
|
|||||||
section = "Logging"
|
section = "Logging"
|
||||||
cli = ["--logger-class"]
|
cli = ["--logger-class"]
|
||||||
meta = "STRING"
|
meta = "STRING"
|
||||||
validator = validate_string
|
validator = validate_class
|
||||||
default = "simple"
|
default = "simple"
|
||||||
desc = """\
|
desc = """\
|
||||||
The logger you want to use to log events in gunicorn.
|
The logger you want to use to log events in gunicorn.
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import socket
|
|||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
import time
|
import time
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
|
||||||
MAXFD = 1024
|
MAXFD = 1024
|
||||||
@ -103,6 +104,8 @@ relative import to an absolute import.
|
|||||||
return sys.modules[name]
|
return sys.modules[name]
|
||||||
|
|
||||||
def load_class(uri, default="sync", section="gunicorn.workers"):
|
def load_class(uri, default="sync", section="gunicorn.workers"):
|
||||||
|
if inspect.isclass(uri):
|
||||||
|
return uri
|
||||||
if uri.startswith("egg:"):
|
if uri.startswith("egg:"):
|
||||||
# uses entry points
|
# uses entry points
|
||||||
entry_str = uri.split("egg:")[1]
|
entry_str = uri.split("egg:")[1]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user