From f35442ad1b1e78007e5daf1c5172f3c90dea70b9 Mon Sep 17 00:00:00 2001 From: messense Date: Sun, 12 Mar 2017 23:06:44 +0800 Subject: [PATCH] Fix RuntimeError: this event loop is already running --- sanic/worker.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sanic/worker.py b/sanic/worker.py index cd1a6b9f..a54a5a11 100644 --- a/sanic/worker.py +++ b/sanic/worker.py @@ -36,10 +36,12 @@ class GunicornWorker(base.Worker): super().init_process() def run(self): - self._runner = asyncio.async(self._run(), loop=self.loop) + self._runner = asyncio.ensure_future(self._run(), loop=self.loop) try: self.loop.run_until_complete(self._runner) + trigger_events(self._server_settings.get('after_start', []), self.loop) + self.loop.run_until_complete(self._check_alive()) finally: trigger_events(self._server_settings.get('before_stop', []), self.loop) self.loop.close() @@ -83,7 +85,7 @@ class GunicornWorker(base.Worker): **self._server_settings )) - trigger_events(self._server_settings.get('after_start', []), self.loop) + async def _check_alive(self): # If our parent changed then we shut down. pid = os.getpid() try: