MagicStack / MagicStack/uvloop
How to stop event loop in child thread ?
未关闭
还没有人认领这个 Issue。
- 主要语言
- Cython
- 星标
- 11.9k
- 派生
- 615
- PR 合并指标
- 30 天内没有已合并 PR
描述
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:
贡献指南
这个仓库没有索引到贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先,使用提供的 Python 3.7.2、uvloop 0.14.0 和 macOS 环境,复现 _thread_main、run_forever 与 stop 之间的交互。检查 issue 中所示的 loop 和 server 生命周期调用,然后确认从父线程停止是否能够正常完成。记录受支持的行为或所需的更改。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 30/100