2016-11-08 22:00 GMT+01:00 Guido van Rossum <[email protected]>: > But they could also call it before a loop is active and then call > run_until_complete() on the thing. E.g. > > f = sleep1() # definition from my previous post > asyncio.get_event_loop().run_until_complete(f) > > Assuming that's the only get_event_loop() call in the program, this will > work if sleep1() is an actual coroutine or generator, but not if it returns > a Future or Task. >
Yes, this is it. The example I had in mind was a wrapper function creating a lock and returning a coroutine instance. I wanted Martin to clarify what does he do in his asyncio unittest library >> -- what he tries to mock and why. >> > Asynctest uses introspection to mock coroutine functions and generators correctly: mocked_coroutine_function = asynctest.Mock(asyncio.sleep) await mocked_coroutine_function() works because asynctest.Mock returns an awaitable when called if the spec is a coroutine function. While: mocked_coro_wrapper = asynctest.Mock(sleep1) await mocked_coro_wrapper() # fails with TypeError: the returned mock is not awaitable. We should strive to make more things coroutines (though I'm not sure how to >>> turn gather() into a coroutine -- I recall it was complicated to write, >>> with the variant behaviors and possible timeouts or cancellations). >>> >> >> Can we rename asyncio.gather to asyncio._gather and wrap it into a >> coroutine? >> > > I suppose, though that just complexifies it more. :-( It will solve the > immediate issue being discussed here though. > Why not await the future instead of returning it? @asyncio.coroutine def gather(...): # ... return (yield from outer) > -- > --Guido van Rossum (python.org/~guido) > -- Martin <http://www.martiusweb.net> Richard www.martiusweb.net
