Skip to content

Instantly share code, notes, and snippets.

@ambv
Created June 2, 2016 19:43
Show Gist options
  • Save ambv/3324e3e569b9f7883dfcb9c8cb1d5445 to your computer and use it in GitHub Desktop.
Save ambv/3324e3e569b9f7883dfcb9c8cb1d5445 to your computer and use it in GitHub Desktop.
In which rogue call_soons starve cooperating callbacks.
import asyncio
import time
loop = asyncio.get_event_loop()
async def go(sleep):
await asyncio.sleep(sleep)
print('I slept', sleep, 'seconds')
def clog(sleep):
time.sleep(sleep)
print('I clogged', sleep, 'seconds')
loop.call_at(loop.time() + 0.3, lambda: print('here I am'))
loop.create_task(go(1))
loop.create_task(go(2))
loop.create_task(go(3))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.call_soon(lambda: clog(1))
loop.run_forever()
@gvanrossum
Copy link

I don't know what you're trying to prove here. Of course the event loop is unavailable when a coroutine calls time.sleep().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment