I'm writing a library that I'd like end-users to be able to optionally use as if its methods and functions were not coroutines.
For example, given this function:
@asyncio.coroutine
def blah_getter():
return (yield from http_client.get('http://blahblahblah'))
An end user who doesn't care to use any asynchronous features in their own code, still has to import asyncio and run this:
>>> response = asyncio.get_event_loop().run_until_complete(blah_getter())
It would be cool if I could, inside of blah_getter
determine if I was being called as a coroutine or not and react accordingly.
So something like:
@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'))
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…