asyncio: Error if awaiting in parallel on the same coroutine

See https://github.com/python/asyncio/pull/293 for details.
This commit is contained in:
Yury Selivanov 2015-11-18 12:40:26 -05:00
commit 576fe71c12
2 changed files with 27 additions and 1 deletions

View File

@ -140,7 +140,13 @@ class CoroWrapper:
if compat.PY35:
__await__ = __iter__ # make compatible with 'await' expression
def __await__(self):
cr_await = getattr(self.gen, 'cr_await', None)
if cr_await is not None:
raise RuntimeError(
"Cannot await on coroutine {!r} while it's "
"awaiting for {!r}".format(self.gen, cr_await))
return self
@property
def gi_yieldfrom(self):

View File

@ -203,6 +203,26 @@ class CoroutineTests(BaseTest):
self.loop.run_until_complete(runner())
def test_double_await(self):
async def afunc():
await asyncio.sleep(0.1, loop=self.loop)
async def runner():
coro = afunc()
t = asyncio.Task(coro, loop=self.loop)
try:
await asyncio.sleep(0, loop=self.loop)
await coro
finally:
t.cancel()
self.loop.set_debug(True)
with self.assertRaisesRegex(
RuntimeError,
r'Cannot await.*test_double_await.*\bafunc\b.*while.*\bsleep\b'):
self.loop.run_until_complete(runner())
if __name__ == '__main__':
unittest.main()