Last active
March 16, 2020 12:55
-
-
Save bloodywing/350c62ff339249692d32ab15fe3f8c68 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 asyncio | |
| import time | |
| from threading import Thread | |
| def a_blocking_function(): | |
| time.sleep(10) | |
| print('a_blocking_function sleep for 10 seconds') | |
| async def another_function_that_blocks(): | |
| print('You will see me more often') | |
| async def main(secondary_loop): | |
| while True: | |
| await asyncio.sleep(1) | |
| print('Main loop - noop') | |
| await another_function_that_blocks() | |
| secondary_loop.call_soon_threadsafe(a_blocking_function) | |
| def secondary_thread(secondary_loop): | |
| asyncio.set_event_loop(secondary_loop) | |
| secondary_loop.run_forever() | |
| if __name__ == '__main__': | |
| secondary_loop = asyncio.new_event_loop() | |
| t = Thread(target=secondary_thread, args=(secondary_loop,)) | |
| t.start() | |
| asyncio.run(main(secondary_loop)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment