uvloop icon indicating copy to clipboard operation
uvloop copied to clipboard

How to stop event loop in child thread ?

Open csrftoken opened this issue 4 years ago • 2 comments

I call the stop method in child thread,but loop’s state is running ~ Can you help me ?

import time
import asyncio
import threading
from functools import partial

import sanic
import uvloop


class ApiService:

    def __init__(self):
        self.__server = None
        self.__loop = None

    def _thread_main(self, loop):
        app = sanic.Sanic("Api")
        asyncio.set_event_loop(loop)
        serv_coro = app.create_server(
            host="0.0.0.0", port=8080, return_asyncio_server=True, debug=False
        )

        serv_task = asyncio.ensure_future(serv_coro, loop=self.__loop)
        self.__server = self.__loop.run_until_complete(serv_task)
        self.__server.after_start()
        self.__loop.run_forever()

    def run(self):
        self.__loop = uvloop.new_event_loop()
        # print(dir(self.__loop))
        # self._thread_main(self.__loop)
        thread = threading.Thread(
            target=partial(self._thread_main, self.__loop), name="api-service"
        )
        thread.setDaemon(True)
        thread.start()
        # thread.join()

    def stop(self):
        if self.__loop is None or self.__server is None:
            return

        self.__loop.stop()
        self.__server.before_stop()
        print(self.__loop)
        # Wait for server to close
        self.__loop.run_until_complete(self.__server.close())

        # Complete all tasks on the loop
        for connection in self.__server.connections:
            connection.close_if_idle()
        self.__server.after_stop()


if __name__ == '__main__':

    app = ApiService()
    try:
        app.run()
        while True:
            time.sleep(10)
    except (KeyboardInterrupt, InterruptedError) as _:
        print(app._ApiService__loop)
    except Exception as e:
        pass
    finally:
        app.stop()
  • uvloop:0.14.0:
  • python:3.7.2:
  • MacOS10.15.7:

csrftoken avatar Nov 23 '20 02:11 csrftoken

The error is:

Traceback (most recent call last):
  File "", line 71, in <module>
    app.stop()
  File "", line 51, in stop
    self.__loop.run_until_complete(self.__server.close())
  File "uvloop/loop.pyx", line 1450, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1443, in uvloop.loop.Loop.run_until_complete
  File "uvloop/loop.pyx", line 1351, in uvloop.loop.Loop.run_forever
  File "uvloop/loop.pyx", line 480, in uvloop.loop.Loop._run
RuntimeError: this event loop is already running.

csrftoken avatar Nov 23 '20 02:11 csrftoken

loop.run_until_complete only works when the event loop is NOT running. Otherwise asyncio.ensure_future or similar or simply await must be used

TECHNOFAB11 avatar Dec 13 '20 16:12 TECHNOFAB11