mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Merge pull request #1784 from tnir/1599-pycodestyle-ci
Enable pycodestyle
This commit is contained in:
commit
e636bf8198
@ -142,7 +142,7 @@ class Application(BaseApplication):
|
|||||||
continue
|
continue
|
||||||
try:
|
try:
|
||||||
self.cfg.set(k.lower(), v)
|
self.cfg.set(k.lower(), v)
|
||||||
except:
|
except Exception:
|
||||||
print("Invalid value for %s: %s\n" % (k, v), file=sys.stderr)
|
print("Invalid value for %s: %s\n" % (k, v), file=sys.stderr)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
raise
|
raise
|
||||||
@ -203,7 +203,7 @@ class Application(BaseApplication):
|
|||||||
if self.cfg.check_config:
|
if self.cfg.check_config:
|
||||||
try:
|
try:
|
||||||
self.load()
|
self.load()
|
||||||
except:
|
except Exception:
|
||||||
msg = "\nError while loading the application:\n"
|
msg = "\nError while loading the application:\n"
|
||||||
print(msg, file=sys.stderr)
|
print(msg, file=sys.stderr)
|
||||||
traceback.print_exc()
|
traceback.print_exc()
|
||||||
|
|||||||
@ -590,7 +590,7 @@ class Arbiter(object):
|
|||||||
print("%s" % e, file=sys.stderr)
|
print("%s" % e, file=sys.stderr)
|
||||||
sys.stderr.flush()
|
sys.stderr.flush()
|
||||||
sys.exit(self.APP_LOAD_ERROR)
|
sys.exit(self.APP_LOAD_ERROR)
|
||||||
except:
|
except Exception:
|
||||||
self.log.exception("Exception in worker process")
|
self.log.exception("Exception in worker process")
|
||||||
if not worker.booted:
|
if not worker.booted:
|
||||||
sys.exit(self.WORKER_BOOT_ERROR)
|
sys.exit(self.WORKER_BOOT_ERROR)
|
||||||
@ -600,9 +600,9 @@ class Arbiter(object):
|
|||||||
try:
|
try:
|
||||||
worker.tmp.close()
|
worker.tmp.close()
|
||||||
self.cfg.worker_exit(self, worker)
|
self.cfg.worker_exit(self, worker)
|
||||||
except:
|
except Exception:
|
||||||
self.log.warning("Exception during worker exit:\n%s",
|
self.log.warning("Exception during worker exit:\n%s",
|
||||||
traceback.format_exc())
|
traceback.format_exc())
|
||||||
|
|
||||||
def spawn_workers(self):
|
def spawn_workers(self):
|
||||||
"""\
|
"""\
|
||||||
|
|||||||
@ -78,9 +78,9 @@ class Config(object):
|
|||||||
}
|
}
|
||||||
parser = argparse.ArgumentParser(**kwargs)
|
parser = argparse.ArgumentParser(**kwargs)
|
||||||
parser.add_argument("-v", "--version",
|
parser.add_argument("-v", "--version",
|
||||||
action="version", default=argparse.SUPPRESS,
|
action="version", default=argparse.SUPPRESS,
|
||||||
version="%(prog)s (version " + __version__ + ")\n",
|
version="%(prog)s (version " + __version__ + ")\n",
|
||||||
help="show program's version number and exit")
|
help="show program's version number and exit")
|
||||||
parser.add_argument("args", nargs="*", help=argparse.SUPPRESS)
|
parser.add_argument("args", nargs="*", help=argparse.SUPPRESS)
|
||||||
|
|
||||||
keys = sorted(self.settings, key=self.settings.__getitem__)
|
keys = sorted(self.settings, key=self.settings.__getitem__)
|
||||||
@ -93,7 +93,7 @@ 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?
|
# are we using a threaded worker?
|
||||||
is_sync = uri.endswith('SyncWorker') or uri == 'sync'
|
is_sync = uri.endswith('SyncWorker') or uri == 'sync'
|
||||||
if is_sync and self.threads > 1:
|
if is_sync and self.threads > 1:
|
||||||
return "threads"
|
return "threads"
|
||||||
@ -103,7 +103,7 @@ class Config(object):
|
|||||||
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 = 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"
|
||||||
@ -524,7 +524,7 @@ def validate_reload_engine(val):
|
|||||||
|
|
||||||
def get_default_config_file():
|
def get_default_config_file():
|
||||||
config_path = os.path.join(os.path.abspath(os.getcwd()),
|
config_path = os.path.join(os.path.abspath(os.getcwd()),
|
||||||
'gunicorn.conf.py')
|
'gunicorn.conf.py')
|
||||||
if os.path.exists(config_path):
|
if os.path.exists(config_path):
|
||||||
return config_path
|
return config_path
|
||||||
return None
|
return None
|
||||||
@ -550,6 +550,7 @@ class ConfigFile(Setting):
|
|||||||
prefix.
|
prefix.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Bind(Setting):
|
class Bind(Setting):
|
||||||
name = "bind"
|
name = "bind"
|
||||||
action = "append"
|
action = "append"
|
||||||
@ -654,6 +655,7 @@ class WorkerClass(Setting):
|
|||||||
``gunicorn.workers.ggevent.GeventWorker``.
|
``gunicorn.workers.ggevent.GeventWorker``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class WorkerThreads(Setting):
|
class WorkerThreads(Setting):
|
||||||
name = "threads"
|
name = "threads"
|
||||||
section = "Worker Processes"
|
section = "Worker Processes"
|
||||||
@ -1025,6 +1027,7 @@ class Daemon(Setting):
|
|||||||
background.
|
background.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Env(Setting):
|
class Env(Setting):
|
||||||
name = "raw_env"
|
name = "raw_env"
|
||||||
action = "append"
|
action = "append"
|
||||||
@ -1058,6 +1061,7 @@ class Pidfile(Setting):
|
|||||||
If not set, no PID file will be written.
|
If not set, no PID file will be written.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class WorkerTmpDir(Setting):
|
class WorkerTmpDir(Setting):
|
||||||
name = "worker_tmp_dir"
|
name = "worker_tmp_dir"
|
||||||
section = "Server Mechanics"
|
section = "Server Mechanics"
|
||||||
@ -1111,6 +1115,7 @@ class Group(Setting):
|
|||||||
change the worker processes group.
|
change the worker processes group.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class Umask(Setting):
|
class Umask(Setting):
|
||||||
name = "umask"
|
name = "umask"
|
||||||
section = "Server Mechanics"
|
section = "Server Mechanics"
|
||||||
@ -1224,6 +1229,7 @@ class AccessLog(Setting):
|
|||||||
``'-'`` means log to stdout.
|
``'-'`` means log to stdout.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class DisableRedirectAccessToSyslog(Setting):
|
class DisableRedirectAccessToSyslog(Setting):
|
||||||
name = "disable_redirect_access_to_syslog"
|
name = "disable_redirect_access_to_syslog"
|
||||||
section = "Logging"
|
section = "Logging"
|
||||||
@ -1676,6 +1682,7 @@ class PostWorkerInit(Setting):
|
|||||||
Worker.
|
Worker.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class WorkerInt(Setting):
|
class WorkerInt(Setting):
|
||||||
name = "worker_int"
|
name = "worker_int"
|
||||||
section = "Server Hooks"
|
section = "Server Hooks"
|
||||||
@ -1819,6 +1826,7 @@ class NumWorkersChanged(Setting):
|
|||||||
be ``None``.
|
be ``None``.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class OnExit(Setting):
|
class OnExit(Setting):
|
||||||
name = "on_exit"
|
name = "on_exit"
|
||||||
section = "Server Hooks"
|
section = "Server Hooks"
|
||||||
@ -1899,6 +1907,7 @@ class CertFile(Setting):
|
|||||||
SSL certificate file
|
SSL certificate file
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class SSLVersion(Setting):
|
class SSLVersion(Setting):
|
||||||
name = "ssl_version"
|
name = "ssl_version"
|
||||||
section = "SSL"
|
section = "SSL"
|
||||||
@ -1945,6 +1954,7 @@ class SSLVersion(Setting):
|
|||||||
constants.
|
constants.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class CertReqs(Setting):
|
class CertReqs(Setting):
|
||||||
name = "cert_reqs"
|
name = "cert_reqs"
|
||||||
section = "SSL"
|
section = "SSL"
|
||||||
@ -1955,6 +1965,7 @@ class CertReqs(Setting):
|
|||||||
Whether client certificate is required (see stdlib ssl module's)
|
Whether client certificate is required (see stdlib ssl module's)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class CACerts(Setting):
|
class CACerts(Setting):
|
||||||
name = "ca_certs"
|
name = "ca_certs"
|
||||||
section = "SSL"
|
section = "SSL"
|
||||||
@ -1966,6 +1977,7 @@ class CACerts(Setting):
|
|||||||
CA certificates file
|
CA certificates file
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class SuppressRaggedEOFs(Setting):
|
class SuppressRaggedEOFs(Setting):
|
||||||
name = "suppress_ragged_eofs"
|
name = "suppress_ragged_eofs"
|
||||||
section = "SSL"
|
section = "SSL"
|
||||||
@ -1977,6 +1989,7 @@ class SuppressRaggedEOFs(Setting):
|
|||||||
Suppress ragged EOFs (see stdlib ssl module's)
|
Suppress ragged EOFs (see stdlib ssl module's)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
class DoHandshakeOnConnect(Setting):
|
class DoHandshakeOnConnect(Setting):
|
||||||
name = "do_handshake_on_connect"
|
name = "do_handshake_on_connect"
|
||||||
section = "SSL"
|
section = "SSL"
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import base64
|
|||||||
import binascii
|
import binascii
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
logging.Logger.manager.emittedNoHandlerWarning = 1
|
logging.Logger.manager.emittedNoHandlerWarning = 1 # noqa
|
||||||
from logging.config import dictConfig
|
from logging.config import dictConfig
|
||||||
from logging.config import fileConfig
|
from logging.config import fileConfig
|
||||||
import os
|
import os
|
||||||
@ -21,28 +21,28 @@ from gunicorn import util
|
|||||||
|
|
||||||
# syslog facility codes
|
# syslog facility codes
|
||||||
SYSLOG_FACILITIES = {
|
SYSLOG_FACILITIES = {
|
||||||
"auth": 4,
|
"auth": 4,
|
||||||
"authpriv": 10,
|
"authpriv": 10,
|
||||||
"cron": 9,
|
"cron": 9,
|
||||||
"daemon": 3,
|
"daemon": 3,
|
||||||
"ftp": 11,
|
"ftp": 11,
|
||||||
"kern": 0,
|
"kern": 0,
|
||||||
"lpr": 6,
|
"lpr": 6,
|
||||||
"mail": 2,
|
"mail": 2,
|
||||||
"news": 7,
|
"news": 7,
|
||||||
"security": 4, # DEPRECATED
|
"security": 4, # DEPRECATED
|
||||||
"syslog": 5,
|
"syslog": 5,
|
||||||
"user": 1,
|
"user": 1,
|
||||||
"uucp": 8,
|
"uucp": 8,
|
||||||
"local0": 16,
|
"local0": 16,
|
||||||
"local1": 17,
|
"local1": 17,
|
||||||
"local2": 18,
|
"local2": 18,
|
||||||
"local3": 19,
|
"local3": 19,
|
||||||
"local4": 20,
|
"local4": 20,
|
||||||
"local5": 21,
|
"local5": 21,
|
||||||
"local6": 22,
|
"local6": 22,
|
||||||
"local7": 23
|
"local7": 23
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CONFIG_DEFAULTS = dict(
|
CONFIG_DEFAULTS = dict(
|
||||||
@ -213,8 +213,10 @@ class Logger(object):
|
|||||||
|
|
||||||
# set gunicorn.access handler
|
# set gunicorn.access handler
|
||||||
if cfg.accesslog is not None:
|
if cfg.accesslog is not None:
|
||||||
self._set_handler(self.access_log, cfg.accesslog,
|
self._set_handler(
|
||||||
fmt=logging.Formatter(self.access_fmt), stream=sys.stdout)
|
self.access_log, cfg.accesslog,
|
||||||
|
fmt=logging.Formatter(self.access_fmt), stream=sys.stdout
|
||||||
|
)
|
||||||
|
|
||||||
# set syslog handler
|
# set syslog handler
|
||||||
if cfg.syslog:
|
if cfg.syslog:
|
||||||
@ -284,7 +286,8 @@ class Logger(object):
|
|||||||
'u': self._get_user(environ) or '-',
|
'u': self._get_user(environ) or '-',
|
||||||
't': self.now(),
|
't': self.now(),
|
||||||
'r': "%s %s %s" % (environ['REQUEST_METHOD'],
|
'r': "%s %s %s" % (environ['REQUEST_METHOD'],
|
||||||
environ['RAW_URI'], environ["SERVER_PROTOCOL"]),
|
environ['RAW_URI'],
|
||||||
|
environ["SERVER_PROTOCOL"]),
|
||||||
's': status,
|
's': status,
|
||||||
'm': environ.get('REQUEST_METHOD'),
|
'm': environ.get('REQUEST_METHOD'),
|
||||||
'U': environ.get('PATH_INFO'),
|
'U': environ.get('PATH_INFO'),
|
||||||
@ -295,7 +298,7 @@ class Logger(object):
|
|||||||
'f': environ.get('HTTP_REFERER', '-'),
|
'f': environ.get('HTTP_REFERER', '-'),
|
||||||
'a': environ.get('HTTP_USER_AGENT', '-'),
|
'a': environ.get('HTTP_USER_AGENT', '-'),
|
||||||
'T': request_time.seconds,
|
'T': request_time.seconds,
|
||||||
'D': (request_time.seconds*1000000) + request_time.microseconds,
|
'D': (request_time.seconds * 1000000) + request_time.microseconds,
|
||||||
'L': "%d.%06d" % (request_time.seconds, request_time.microseconds),
|
'L': "%d.%06d" % (request_time.seconds, request_time.microseconds),
|
||||||
'p': "<%s>" % os.getpid()
|
'p': "<%s>" % os.getpid()
|
||||||
}
|
}
|
||||||
@ -337,12 +340,13 @@ class Logger(object):
|
|||||||
# wrap atoms:
|
# wrap atoms:
|
||||||
# - make sure atoms will be test case insensitively
|
# - make sure atoms will be test case insensitively
|
||||||
# - if atom doesn't exist replace it by '-'
|
# - if atom doesn't exist replace it by '-'
|
||||||
safe_atoms = self.atoms_wrapper_class(self.atoms(resp, req, environ,
|
safe_atoms = self.atoms_wrapper_class(
|
||||||
request_time))
|
self.atoms(resp, req, environ, request_time)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.access_log.info(self.cfg.access_log_format, safe_atoms)
|
self.access_log.info(self.cfg.access_log_format, safe_atoms)
|
||||||
except:
|
except Exception:
|
||||||
self.error(traceback.format_exc())
|
self.error(traceback.format_exc())
|
||||||
|
|
||||||
def now(self):
|
def now(self):
|
||||||
@ -361,7 +365,6 @@ class Logger(object):
|
|||||||
os.dup2(self.logfile.fileno(), sys.stdout.fileno())
|
os.dup2(self.logfile.fileno(), sys.stdout.fileno())
|
||||||
os.dup2(self.logfile.fileno(), sys.stderr.fileno())
|
os.dup2(self.logfile.fileno(), sys.stderr.fileno())
|
||||||
|
|
||||||
|
|
||||||
for log in loggers():
|
for log in loggers():
|
||||||
for handler in log.handlers:
|
for handler in log.handlers:
|
||||||
if isinstance(handler, logging.FileHandler):
|
if isinstance(handler, logging.FileHandler):
|
||||||
|
|||||||
@ -10,9 +10,11 @@ from errno import ENOTCONN
|
|||||||
|
|
||||||
from gunicorn.http.unreader import SocketUnreader
|
from gunicorn.http.unreader import SocketUnreader
|
||||||
from gunicorn.http.body import ChunkedReader, LengthReader, EOFReader, Body
|
from gunicorn.http.body import ChunkedReader, LengthReader, EOFReader, Body
|
||||||
from gunicorn.http.errors import (InvalidHeader, InvalidHeaderName, NoMoreData,
|
from gunicorn.http.errors import (
|
||||||
|
InvalidHeader, InvalidHeaderName, NoMoreData,
|
||||||
InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion,
|
InvalidRequestLine, InvalidRequestMethod, InvalidHTTPVersion,
|
||||||
LimitRequestLine, LimitRequestHeaders)
|
LimitRequestLine, LimitRequestHeaders,
|
||||||
|
)
|
||||||
from gunicorn.http.errors import InvalidProxyLine, ForbiddenProxyRequest
|
from gunicorn.http.errors import InvalidProxyLine, ForbiddenProxyRequest
|
||||||
from gunicorn.http.errors import InvalidSchemeHeaders
|
from gunicorn.http.errors import InvalidSchemeHeaders
|
||||||
from gunicorn.util import bytes_to_str, split_request_uri
|
from gunicorn.util import bytes_to_str, split_request_uri
|
||||||
@ -105,7 +107,7 @@ class Message(object):
|
|||||||
header_length += len(curr)
|
header_length += len(curr)
|
||||||
if header_length > self.limit_request_field_size > 0:
|
if header_length > self.limit_request_field_size > 0:
|
||||||
raise LimitRequestHeaders("limit request headers "
|
raise LimitRequestHeaders("limit request headers "
|
||||||
+ "fields size")
|
"fields size")
|
||||||
value.append(curr)
|
value.append(curr)
|
||||||
value = ''.join(value).rstrip()
|
value = ''.join(value).rstrip()
|
||||||
|
|
||||||
|
|||||||
@ -303,7 +303,7 @@ class Response(object):
|
|||||||
|
|
||||||
headers = [
|
headers = [
|
||||||
"HTTP/%s.%s %s\r\n" % (self.req.version[0],
|
"HTTP/%s.%s %s\r\n" % (self.req.version[0],
|
||||||
self.req.version[1], self.status),
|
self.req.version[1], self.status),
|
||||||
"Server: %s\r\n" % self.version,
|
"Server: %s\r\n" % self.version,
|
||||||
"Date: %s\r\n" % util.http_date(),
|
"Date: %s\r\n" % util.http_date(),
|
||||||
"Connection: %s\r\n" % connection
|
"Connection: %s\r\n" % connection
|
||||||
|
|||||||
@ -19,6 +19,7 @@ GAUGE_TYPE = "gauge"
|
|||||||
COUNTER_TYPE = "counter"
|
COUNTER_TYPE = "counter"
|
||||||
HISTOGRAM_TYPE = "histogram"
|
HISTOGRAM_TYPE = "histogram"
|
||||||
|
|
||||||
|
|
||||||
class Statsd(Logger):
|
class Statsd(Logger):
|
||||||
"""statsD-based instrumentation, that passes as a logger
|
"""statsD-based instrumentation, that passes as a logger
|
||||||
"""
|
"""
|
||||||
|
|||||||
@ -57,7 +57,7 @@ class Pidfile(object):
|
|||||||
|
|
||||||
if pid1 == self.pid:
|
if pid1 == self.pid:
|
||||||
os.unlink(self.fname)
|
os.unlink(self.fname)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
|
|||||||
@ -53,6 +53,7 @@ class Reloader(threading.Thread):
|
|||||||
self._callback(filename)
|
self._callback(filename)
|
||||||
time.sleep(self._interval)
|
time.sleep(self._interval)
|
||||||
|
|
||||||
|
|
||||||
has_inotify = False
|
has_inotify = False
|
||||||
if sys.platform.startswith('linux'):
|
if sys.platform.startswith('linux'):
|
||||||
try:
|
try:
|
||||||
|
|||||||
@ -56,7 +56,7 @@ except ImportError:
|
|||||||
|
|
||||||
|
|
||||||
def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
|
def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
|
||||||
section="gunicorn.workers"):
|
section="gunicorn.workers"):
|
||||||
if inspect.isclass(uri):
|
if inspect.isclass(uri):
|
||||||
return uri
|
return uri
|
||||||
if uri.startswith("egg:"):
|
if uri.startswith("egg:"):
|
||||||
@ -70,7 +70,7 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
return pkg_resources.load_entry_point(dist, section, name)
|
return pkg_resources.load_entry_point(dist, section, name)
|
||||||
except:
|
except Exception:
|
||||||
exc = traceback.format_exc()
|
exc = traceback.format_exc()
|
||||||
msg = "class uri %r invalid or not found: \n\n[%s]"
|
msg = "class uri %r invalid or not found: \n\n[%s]"
|
||||||
raise RuntimeError(msg % (uri, exc))
|
raise RuntimeError(msg % (uri, exc))
|
||||||
@ -86,9 +86,10 @@ def load_class(uri, default="gunicorn.workers.sync.SyncWorker",
|
|||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return pkg_resources.load_entry_point("gunicorn",
|
return pkg_resources.load_entry_point(
|
||||||
section, uri)
|
"gunicorn", section, uri
|
||||||
except:
|
)
|
||||||
|
except Exception:
|
||||||
exc = traceback.format_exc()
|
exc = traceback.format_exc()
|
||||||
msg = "class uri %r invalid or not found: \n\n[%s]"
|
msg = "class uri %r invalid or not found: \n\n[%s]"
|
||||||
raise RuntimeError(msg % (uri, exc))
|
raise RuntimeError(msg % (uri, exc))
|
||||||
@ -260,6 +261,7 @@ def close(sock):
|
|||||||
except socket.error:
|
except socket.error:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from os import closerange
|
from os import closerange
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@ -439,7 +441,7 @@ def getcwd():
|
|||||||
cwd = os.environ['PWD']
|
cwd = os.environ['PWD']
|
||||||
else:
|
else:
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
except:
|
except Exception:
|
||||||
cwd = os.getcwd()
|
cwd = os.getcwd()
|
||||||
return cwd
|
return cwd
|
||||||
|
|
||||||
|
|||||||
@ -28,8 +28,9 @@ from gunicorn.workers.workertmp import WorkerTmp
|
|||||||
|
|
||||||
class Worker(object):
|
class Worker(object):
|
||||||
|
|
||||||
SIGNALS = [getattr(signal, "SIG%s" % x)
|
SIGNALS = [getattr(signal, "SIG%s" % x) for x in (
|
||||||
for x in "ABRT HUP QUIT INT TERM USR1 USR2 WINCH CHLD".split()]
|
"ABRT HUP QUIT INT TERM USR1 USR2 WINCH CHLD".split()
|
||||||
|
)]
|
||||||
|
|
||||||
PIPE = []
|
PIPE = []
|
||||||
|
|
||||||
@ -203,12 +204,14 @@ class Worker(object):
|
|||||||
def handle_error(self, req, client, addr, exc):
|
def handle_error(self, req, client, addr, exc):
|
||||||
request_start = datetime.now()
|
request_start = datetime.now()
|
||||||
addr = addr or ('', -1) # unix socket case
|
addr = addr or ('', -1) # unix socket case
|
||||||
if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
|
if isinstance(exc, (
|
||||||
InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,
|
InvalidRequestLine, InvalidRequestMethod,
|
||||||
LimitRequestLine, LimitRequestHeaders,
|
InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,
|
||||||
InvalidProxyLine, ForbiddenProxyRequest,
|
LimitRequestLine, LimitRequestHeaders,
|
||||||
InvalidSchemeHeaders,
|
InvalidProxyLine, ForbiddenProxyRequest,
|
||||||
SSLError)):
|
InvalidSchemeHeaders,
|
||||||
|
SSLError,
|
||||||
|
)):
|
||||||
|
|
||||||
status_int = 400
|
status_int = 400
|
||||||
reason = "Bad Request"
|
reason = "Bad Request"
|
||||||
@ -261,7 +264,7 @@ class Worker(object):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
util.write_error(client, status_int, reason, mesg)
|
util.write_error(client, status_int, reason, mesg)
|
||||||
except:
|
except Exception:
|
||||||
self.log.debug("Failed to send error message.")
|
self.log.debug("Failed to send error message.")
|
||||||
|
|
||||||
def handle_winch(self, sig, fname):
|
def handle_winch(self, sig, fname):
|
||||||
|
|||||||
@ -92,7 +92,7 @@ class AsyncWorker(base.Worker):
|
|||||||
try:
|
try:
|
||||||
self.cfg.pre_request(self, req)
|
self.cfg.pre_request(self, req)
|
||||||
resp, environ = wsgi.create(req, sock, addr,
|
resp, environ = wsgi.create(req, sock, addr,
|
||||||
listener_name, self.cfg)
|
listener_name, self.cfg)
|
||||||
environ["wsgi.multithread"] = True
|
environ["wsgi.multithread"] = True
|
||||||
self.nr += 1
|
self.nr += 1
|
||||||
if self.alive and self.nr >= self.max_requests:
|
if self.alive and self.nr >= self.max_requests:
|
||||||
|
|||||||
@ -40,6 +40,7 @@ def _gevent_sendfile(fdout, fdin, offset, nbytes, _os_sendfile=os.sendfile):
|
|||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
def patch_sendfile():
|
def patch_sendfile():
|
||||||
setattr(os, "sendfile", _gevent_sendfile)
|
setattr(os, "sendfile", _gevent_sendfile)
|
||||||
|
|
||||||
@ -129,7 +130,7 @@ class GeventWorker(AsyncWorker):
|
|||||||
self.log.warning("Worker graceful timeout (pid:%s)" % self.pid)
|
self.log.warning("Worker graceful timeout (pid:%s)" % self.pid)
|
||||||
for server in servers:
|
for server in servers:
|
||||||
server.stop(timeout=1)
|
server.stop(timeout=1)
|
||||||
except:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def handle(self, listener, client, addr):
|
def handle(self, listener, client, addr):
|
||||||
|
|||||||
@ -50,7 +50,7 @@ class TConn(object):
|
|||||||
# wrap the socket if needed
|
# wrap the socket if needed
|
||||||
if self.cfg.is_ssl:
|
if self.cfg.is_ssl:
|
||||||
self.sock = ssl.wrap_socket(self.sock, server_side=True,
|
self.sock = ssl.wrap_socket(self.sock, server_side=True,
|
||||||
**self.cfg.ssl_options)
|
**self.cfg.ssl_options)
|
||||||
|
|
||||||
# initialize the parser
|
# initialize the parser
|
||||||
self.parser = http.RequestParser(self.cfg, self.sock)
|
self.parser = http.RequestParser(self.cfg, self.sock)
|
||||||
@ -83,7 +83,7 @@ class ThreadWorker(base.Worker):
|
|||||||
|
|
||||||
if max_keepalived <= 0 and cfg.keepalive:
|
if max_keepalived <= 0 and cfg.keepalive:
|
||||||
log.warning("No keepalived connections can be handled. " +
|
log.warning("No keepalived connections can be handled. " +
|
||||||
"Check the number of worker connections and threads.")
|
"Check the number of worker connections and threads.")
|
||||||
|
|
||||||
def init_process(self):
|
def init_process(self):
|
||||||
self.tpool = self.get_thread_pool()
|
self.tpool = self.get_thread_pool()
|
||||||
@ -123,8 +123,8 @@ class ThreadWorker(base.Worker):
|
|||||||
# enqueue the job
|
# enqueue the job
|
||||||
self.enqueue_req(conn)
|
self.enqueue_req(conn)
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
if e.errno not in (errno.EAGAIN,
|
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
|
||||||
errno.ECONNABORTED, errno.EWOULDBLOCK):
|
errno.EWOULDBLOCK):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def reuse_connection(self, conn, client):
|
def reuse_connection(self, conn, client):
|
||||||
@ -204,11 +204,11 @@ class ThreadWorker(base.Worker):
|
|||||||
|
|
||||||
# check (but do not wait) for finished requests
|
# check (but do not wait) for finished requests
|
||||||
result = futures.wait(self.futures, timeout=0,
|
result = futures.wait(self.futures, timeout=0,
|
||||||
return_when=futures.FIRST_COMPLETED)
|
return_when=futures.FIRST_COMPLETED)
|
||||||
else:
|
else:
|
||||||
# wait for a request to finish
|
# wait for a request to finish
|
||||||
result = futures.wait(self.futures, timeout=1.0,
|
result = futures.wait(self.futures, timeout=1.0,
|
||||||
return_when=futures.FIRST_COMPLETED)
|
return_when=futures.FIRST_COMPLETED)
|
||||||
|
|
||||||
# clean up finished requests
|
# clean up finished requests
|
||||||
for fut in result.done:
|
for fut in result.done:
|
||||||
@ -249,11 +249,11 @@ class ThreadWorker(base.Worker):
|
|||||||
|
|
||||||
# add the socket to the event loop
|
# add the socket to the event loop
|
||||||
self.poller.register(conn.sock, selectors.EVENT_READ,
|
self.poller.register(conn.sock, selectors.EVENT_READ,
|
||||||
partial(self.reuse_connection, conn))
|
partial(self.reuse_connection, conn))
|
||||||
else:
|
else:
|
||||||
self.nr_conns -= 1
|
self.nr_conns -= 1
|
||||||
conn.close()
|
conn.close()
|
||||||
except:
|
except Exception:
|
||||||
# an exception happened, make sure to close the
|
# an exception happened, make sure to close the
|
||||||
# socket.
|
# socket.
|
||||||
self.nr_conns -= 1
|
self.nr_conns -= 1
|
||||||
@ -304,7 +304,7 @@ class ThreadWorker(base.Worker):
|
|||||||
self.cfg.pre_request(self, req)
|
self.cfg.pre_request(self, req)
|
||||||
request_start = datetime.now()
|
request_start = datetime.now()
|
||||||
resp, environ = wsgi.create(req, conn.sock, conn.client,
|
resp, environ = wsgi.create(req, conn.sock, conn.client,
|
||||||
conn.server, self.cfg)
|
conn.server, self.cfg)
|
||||||
environ["wsgi.multithread"] = True
|
environ["wsgi.multithread"] = True
|
||||||
self.nr += 1
|
self.nr += 1
|
||||||
if self.alive and self.nr >= self.max_requests:
|
if self.alive and self.nr >= self.max_requests:
|
||||||
|
|||||||
@ -17,9 +17,11 @@ import gunicorn.http.wsgi as wsgi
|
|||||||
import gunicorn.util as util
|
import gunicorn.util as util
|
||||||
import gunicorn.workers.base as base
|
import gunicorn.workers.base as base
|
||||||
|
|
||||||
|
|
||||||
class StopWaiting(Exception):
|
class StopWaiting(Exception):
|
||||||
""" exception raised to stop waiting for a connection """
|
""" exception raised to stop waiting for a connection """
|
||||||
|
|
||||||
|
|
||||||
class SyncWorker(base.Worker):
|
class SyncWorker(base.Worker):
|
||||||
|
|
||||||
def accept(self, listener):
|
def accept(self, listener):
|
||||||
@ -72,7 +74,7 @@ class SyncWorker(base.Worker):
|
|||||||
|
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
|
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
|
||||||
errno.EWOULDBLOCK):
|
errno.EWOULDBLOCK):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if not self.is_parent_alive():
|
if not self.is_parent_alive():
|
||||||
@ -101,7 +103,7 @@ class SyncWorker(base.Worker):
|
|||||||
self.accept(listener)
|
self.accept(listener)
|
||||||
except EnvironmentError as e:
|
except EnvironmentError as e:
|
||||||
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
|
if e.errno not in (errno.EAGAIN, errno.ECONNABORTED,
|
||||||
errno.EWOULDBLOCK):
|
errno.EWOULDBLOCK):
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if not self.is_parent_alive():
|
if not self.is_parent_alive():
|
||||||
@ -127,7 +129,7 @@ class SyncWorker(base.Worker):
|
|||||||
try:
|
try:
|
||||||
if self.cfg.is_ssl:
|
if self.cfg.is_ssl:
|
||||||
client = ssl.wrap_socket(client, server_side=True,
|
client = ssl.wrap_socket(client, server_side=True,
|
||||||
**self.cfg.ssl_options)
|
**self.cfg.ssl_options)
|
||||||
|
|
||||||
parser = http.RequestParser(self.cfg, client)
|
parser = http.RequestParser(self.cfg, client)
|
||||||
req = next(parser)
|
req = next(parser)
|
||||||
@ -163,7 +165,7 @@ class SyncWorker(base.Worker):
|
|||||||
self.cfg.pre_request(self, req)
|
self.cfg.pre_request(self, req)
|
||||||
request_start = datetime.now()
|
request_start = datetime.now()
|
||||||
resp, environ = wsgi.create(req, client, addr,
|
resp, environ = wsgi.create(req, client, addr,
|
||||||
listener.getsockname(), self.cfg)
|
listener.getsockname(), self.cfg)
|
||||||
# Force the connection closed until someone shows
|
# Force the connection closed until someone shows
|
||||||
# a buffering proxy that supports Keep-Alive to
|
# a buffering proxy that supports Keep-Alive to
|
||||||
# the backend.
|
# the backend.
|
||||||
|
|||||||
@ -35,7 +35,7 @@ class WorkerTmp(object):
|
|||||||
# In Python 3.8, open() emits RuntimeWarning if buffering=1 for binary mode.
|
# In Python 3.8, open() emits RuntimeWarning if buffering=1 for binary mode.
|
||||||
# Because we never write to this file, pass 0 to switch buffering off.
|
# Because we never write to this file, pass 0 to switch buffering off.
|
||||||
self._tmp = os.fdopen(fd, 'w+b', 0)
|
self._tmp = os.fdopen(fd, 'w+b', 0)
|
||||||
except:
|
except Exception:
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|||||||
11
tox.ini
11
tox.ini
@ -38,3 +38,14 @@ deps =
|
|||||||
commands =
|
commands =
|
||||||
rst-lint README.rst docs/README.rst
|
rst-lint README.rst docs/README.rst
|
||||||
bash -c "(set -o pipefail; rst-lint --encoding utf-8 docs/source/*.rst | grep -v 'Unknown interpreted text role\|Unknown directive type'); test $? == 1"
|
bash -c "(set -o pipefail; rst-lint --encoding utf-8 docs/source/*.rst | grep -v 'Unknown interpreted text role\|Unknown directive type'); test $? == 1"
|
||||||
|
|
||||||
|
[testenv:pycodestyle]
|
||||||
|
commands =
|
||||||
|
pycodestyle gunicorn \
|
||||||
|
--exclude=gunicorn/six.py
|
||||||
|
deps =
|
||||||
|
pycodestyle
|
||||||
|
|
||||||
|
[pycodestyle]
|
||||||
|
max-line-length = 120
|
||||||
|
ignore = E129,W503,W504,W606
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user