print(what)
async def main():
- print('started at', time.strftime('%X'))
+ print(f"started at {time.strftime('%X')}")
await say_after(1, 'hello')
await say_after(2, 'world')
- print('finished at', time.strftime('%X'))
+ print(f"finished at {time.strftime('%X')}")
asyncio.run(main())
task2 = asyncio.create_task(
say_after(2, 'world'))
- print('started at', time.strftime('%X'))
+ print(f"started at {time.strftime('%X')}")
# Wait until both tasks are completed (should take
# around 2 seconds.)
await task1
await task2
- print('finished at', time.strftime('%X'))
+ print(f"finished at {time.strftime('%X')}")
Note that expected output now shows that the snippet runs
1 second faster than before::
print('The coroutine took too long, cancelling the task...')
future.cancel()
except Exception as exc:
- print('The coroutine raised an exception: {!r}'.format(exc))
+ print(f'The coroutine raised an exception: {exc!r}')
else:
- print('The coroutine returned: {!r}'.format(result))
+ print(f'The coroutine returned: {result!r}')
See the :ref:`concurrency and multithreading <asyncio-multithreading>`
section of the documentation.