Skip to content

Instantly share code, notes, and snippets.

@bloodywing
Last active March 16, 2020 12:55
Show Gist options
  • Select an option

  • Save bloodywing/350c62ff339249692d32ab15fe3f8c68 to your computer and use it in GitHub Desktop.

Select an option

Save bloodywing/350c62ff339249692d32ab15fe3f8c68 to your computer and use it in GitHub Desktop.
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