# This will print nothing because the set contains tasks, not f1 and f2. import asyncio async def hello_coroutine(): """Corouting function (returns coroutine object).""" return 'hello' def hello_future(): """Function with deferred result (returns a Future/Task).""" return asyncio.get_event_loop().create_task(hello_coroutine()) async def test(): # Fan-out. awaitable1 = hello_future(); awaitable2 = hello_coroutine(); # add ensure_future() to fix. # Fan-in. done, pending = await asyncio.wait( {awaitable1, awaitable2}, return_when=asyncio.ALL_COMPLETED, ) # Make sure everything is as expected. assert len(pending) == 0 assert len(done) == 2 # Recover results using membership test. assert awaitable1 in done try: assert awaitable2 in done except AssertionError: print('FAIL: coroutine object not in `done`.') # Recover results using await. assert (await awaitable1) == 'hello' try: assert (await awaitable2) == 'hello' except AssertionError: print('FAIL: await on coroutine object did not provide return value.') # NOTE: if we launch 2 coroutines here, the result is worse because we'll # get a pair of futures and we don't know which one matches which # coroutine. asyncio.get_event_loop().run_until_complete(test())