我正在编写一个库,希望最终用户能够 Select 性地使用它,就好像它的方法和功能不是协同程序一样.
例如,给定此函数:
@asyncio.coroutine
def blah_getter():
return (yield from http_client.get('http://blahblahblah'))
最终用户如果不想在自己的代码中使用任何异步功能,仍然需要导入asyncio并运行以下操作:
>>> response = asyncio.get_event_loop().run_until_complete(blah_getter())
如果我能在blah_getter
分钟内确定我是否被称为协同程序,并做出相应的react ,那就太酷了.
比如:
@asyncio.coroutine
def blah_getter():
if magically_determine_if_being_yielded_from():
return (yield from http_client.get('http://blahblahblah'))
else:
el = asyncio.get_event_loop()
return el.run_until_complete(http_client.get('http://blahblahblah'))