Skip to content

Instantly share code, notes, and snippets.

@hoto17296
Created July 18, 2017 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoto17296/1c7ac7abcd2f2c4be00b3e8d584548e6 to your computer and use it in GitHub Desktop.
Save hoto17296/1c7ac7abcd2f2c4be00b3e8d584548e6 to your computer and use it in GitHub Desktop.
Python で マルチスレッド + イベントループ2本
import logging
import asyncio
from threading import Thread
logging.basicConfig(format='%(threadName)s:%(message)s', level=logging.INFO)
logger = logging.getLogger()
async def hello():
while True:
logger.info('Hello')
await asyncio.sleep(1)
def loop_in_thread(coro):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(coro)
loop = asyncio.get_event_loop()
try:
Thread(target=loop_in_thread, args=(hello(),), daemon=True).start()
loop.run_until_complete(hello())
except KeyboardInterrupt:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment