You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
1014 B
Python

7 years ago
"""
Here is a trivial example of us awaiting the result of another coroutine. We did this by using the await
keyword before calling it. The await keyword gives control back to the event loop and registers add_42(23) function
and arguments call to the Task Queue allowing the event loop to schedule other tasks. In this case this
single function is the only Task on the Queue so it will be picked up and executed. Once executed the result is
returned to hello_async allowing it to be resumed by the event loop scheduler.
"""
import asyncio
async def add_42(number):
if not isinstance(number, int):
raise ValueError("You need to supply a number to this bitch")
print("Adding 42")
return 42 + number
async def hello_async():
print("Hello async!")
co_result = await add_42(23)
return co_result
event_loop = asyncio.get_event_loop()
try:
print("Entering event loop")
result = event_loop.run_until_complete(hello_async())
print(result)
finally:
event_loop.close()