Skip to content

Instantly share code, notes, and snippets.

@singulared
Created December 11, 2017 17:26
Show Gist options
  • Save singulared/e18735501558623d76d71fd5fa5892b2 to your computer and use it in GitHub Desktop.
Save singulared/e18735501558623d76d71fd5fa5892b2 to your computer and use it in GitHub Desktop.
loop per thread
import threading
import asyncio
async def task(count, stime):
await asyncio.sleep(stime)
print('%s: %s' % (threading.current_thread().name, count), flush=True)
def multi_event_loops():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
t1 = task(1, 6)
t2 = task(2, 3)
loop.run_until_complete(asyncio.gather(t1, t2))
def creepy():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
t1 = task(10, 25)
loop.run_until_complete(t1)
def main():
threading.Thread(target=creepy).start()
for _ in range(3):
threading.Thread(target=multi_event_loops).start()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment