MagicStack / MagicStack/uvloop

How to stop event loop in child thread ?

Open
#370 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Cython
Stars
11.9k
Forks
616
PR merge metrics
No merged PRs in 30d

Description

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:

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the interaction between _thread_main, run_forever, and stop using the supplied Python 3.7.2, uvloop 0.14.0, and macOS setup. Inspect the loop and server lifecycle calls shown in the issue, then verify whether stopping from the parent thread completes cleanly and document the supported behavior or required change.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.