import asyncio import os import re import signal import sys from types import FrameType from typing import Any, Awaitable, Callable, Optional, Union # noqa
from gunicorn.config import AccessLogFormat as GunicornAccessLogFormat from gunicorn.workers import base
from aiohttp import web
from .helpers import set_result from .web_app import Application from .web_log import AccessLogger
ctx = self._create_ssl_context(self.cfg) if self.cfg.is_ssl elseNone
runner = runner assert runner isnotNone
server = runner.server assert server isnotNone for sock in self.sockets:
site = web.SockSite(
runner,
sock,
ssl_context=ctx,
)
await site.start()
# If our parent changed then we shut down.
pid = os.getpid() try: while self.alive: # type: ignore[has-type]
self.notify()
cnt = server.requests_count if self.max_requests and cnt > self.max_requests:
self.alive = False
self.log.info("Max requests, shutting down: %s", self)
@staticmethod def _create_ssl_context(cfg: Any) -> "SSLContext": """Creates SSLContext instance for usage in asyncio.create_server.
See ssl.SSLSocket.__init__ for more details. """ if ssl isNone: # pragma: no cover raise RuntimeError("SSL is not supported.")
ctx = ssl.SSLContext(cfg.ssl_version)
ctx.load_cert_chain(cfg.certfile, cfg.keyfile)
ctx.verify_mode = cfg.cert_reqs if cfg.ca_certs:
ctx.load_verify_locations(cfg.ca_certs) if cfg.ciphers:
ctx.set_ciphers(cfg.ciphers) return ctx
def _get_valid_log_format(self, source_format: str) -> str: if source_format == self.DEFAULT_GUNICORN_LOG_FORMAT: return self.DEFAULT_AIOHTTP_LOG_FORMAT elif re.search(r"%\([^\)]+\)", source_format): raise ValueError( "Gunicorn's style options in form of `%(name)s` are not " "supported for the log formatting. Please use aiohttp's " "format specification to configure access log formatting: " "http://docs.aiohttp.org/en/stable/logging.html" "#format-specification"
) else: return source_format
class GunicornUVLoopWebWorker(GunicornWebWorker): def init_process(self) -> None: import uvloop
# Close any existing event loop before setting a # new policy.
asyncio.get_event_loop().close()
# Setup uvloop policy, so that every # asyncio.get_event_loop() will create an instance # of uvloop event loop.
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
super().init_process()
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.