我希望有人能在这里帮助我。
我有一个对象,它能够拥有返回协程对象的属性。这工作得很好,但是我有一种情况,我需要从单独线程中的同步代码中获取协程对象的结果,而事件循环当前正在运行。我想出的代码是:
def get_sync(self, key: str, default: typing.Any=None) -> typing.Any:
"""
Get an attribute synchronously and safely.
Note:
This does nothing special if an attribute is synchronous. It only
really has a use for asynchronous attributes. It processes
asynchronous attributes synchronously, blocking everything until
the attribute is processed. This helps when running SQL code that
cannot run asynchronously in coroutines.
Args:
key (str): The Config object's attribute name, as a string.
default (Any): The value to use if the Config …Run Code Online (Sandbox Code Playgroud) multithreading event-loop coroutine python-3.x python-asyncio