Created
December 11, 2017 17:26
-
-
Save singulared/e18735501558623d76d71fd5fa5892b2 to your computer and use it in GitHub Desktop.
loop per thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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