Created
July 18, 2017 10:26
-
-
Save hoto17296/1c7ac7abcd2f2c4be00b3e8d584548e6 to your computer and use it in GitHub Desktop.
Python で マルチスレッド + イベントループ2本
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 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