Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor workers #3471

Merged
merged 4 commits into from
Dec 30, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Work on
asvetlov committed Dec 29, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 453dc482fcd217fa902896af2932184712d831a0
17 changes: 7 additions & 10 deletions aiohttp/worker.py
Original file line number Diff line number Diff line change
@@ -52,7 +52,7 @@ def init_process(self) -> None:
super().init_process()

def run(self) -> None:
self._task = self.loop.create_task(self._run(self.wsgi))
self._task = self.loop.create_task(self._run())

try: # ignore all finalization problems
self.loop.run_until_complete(self._task)
@@ -64,20 +64,17 @@ def run(self) -> None:

sys.exit(self.exit_code)

async def _run(
self,
app: Union[Application, Callable[[], Awaitable[Application]]]
) -> None:
if isinstance(app, Application):
real_app = app
elif asyncio.iscoroutinefunction(app):
real_app = await app()
async def _run(self) -> None:
if isinstance(self.wsgi, Application):
app = self.wsgi
elif asyncio.iscoroutinefunction(self.wsgi):
app = await self.wsgi()
else:
raise RuntimeError("wsgi app should be either Application or "
"async function returning Application, got {}"
.format(app))
access_log = self.log.access_log if self.cfg.accesslog else None
runner = web.AppRunner(real_app,
runner = web.AppRunner(app,
logger=self.log,
keepalive_timeout=self.cfg.keepalive,
access_log=access_log,
41 changes: 2 additions & 39 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
@@ -42,7 +42,8 @@ def __init__(self):
self.wsgi = web.Application()


class AsyncioWorker(BaseTestWorker, base_worker.GunicornWebWorker): # type: ignore # noqa
class AsyncioWorker(BaseTestWorker, # type: ignore
base_worker.GunicornWebWorker):
pass


@@ -197,15 +198,11 @@ async def test__run_ok_parent_changed(worker, loop,
worker.cfg.max_requests = 0
worker.cfg.is_ssl = False

worker._runner = web.AppRunner(worker.wsgi)
await worker._runner.setup()

await worker._run()

worker.notify.assert_called_with()
worker.log.info.assert_called_with("Parent changed, shutting down: %s",
worker)
assert worker._runner.server is None


async def test__run_exc(worker, loop, aiohttp_unused_port) -> None:
@@ -223,9 +220,6 @@ async def test__run_exc(worker, loop, aiohttp_unused_port) -> None:
worker.cfg.max_requests = 0
worker.cfg.is_ssl = False

worker._runner = web.AppRunner(worker.wsgi)
await worker._runner.setup()

def raiser():
waiter = worker._notify_waiter
worker.alive = False
@@ -235,37 +229,6 @@ def raiser():
await worker._run()

worker.notify.assert_called_with()
assert worker._runner.server is None


async def test__run_ok_max_requests_exceeded(worker, loop,
aiohttp_unused_port):
skip_if_no_dict(loop)

worker.ppid = os.getppid()
worker.alive = True
worker.servers = {}
sock = socket.socket()
addr = ('localhost', aiohttp_unused_port())
sock.bind(addr)
worker.sockets = [sock]
worker.log = mock.Mock()
worker.loop = loop
worker.cfg.access_log_format = ACCEPTABLE_LOG_FORMAT
worker.cfg.max_requests = 10
worker.cfg.is_ssl = False

worker._runner = web.AppRunner(worker.wsgi)
await worker._runner.setup()
worker._runner.server.requests_count = 30

await worker._run()

worker.notify.assert_called_with()
worker.log.info.assert_called_with("Max requests, shutting down: %s",
worker)

assert worker._runner.server is None


def test__create_ssl_context_without_certs_and_ciphers(worker) -> None: