blivechat/utils/async_io.py
2024-01-25 20:11:42 +08:00

14 lines
345 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
import asyncio
# 只用于持有Task的引用
_task_refs = set()
def create_task_with_ref(*args, **kwargs):
"""创建Task并保持引用防止协程执行完之前就被GC"""
task = asyncio.create_task(*args, **kwargs)
_task_refs.add(task)
task.add_done_callback(_task_refs.discard)
return task