Mercurial > code > home > repos > light9
view blender/asyncio_thread.py @ 2458:0e27ba33118c default tip
better blender<->asco playback cooperation. still no play support in blender; only seek
author | drewp@bigasterisk.com |
---|---|
date | Tue, 20 May 2025 16:25:06 -0700 |
parents | 405abed9a45c |
children |
line wrap: on
line source
import asyncio import sys from threading import Thread, get_ident import time from typing import Coroutine def log(msg): try: rl = hex(id(asyncio.get_running_loop())) except RuntimeError: rl = '(no loop ----)' sys.stderr.write(f"thread={hex(get_ident())} loop={rl} {msg}\n") def startLoopInThread(task: Coroutine) -> asyncio.AbstractEventLoop: """ run a new event loop in a background thread. `task` is run in the new loop. Caller should use this (from fg thread) to run further tasks: asyncio.run_coroutine_threadsafe(task, returned_loop) """ log('🚋4 startLoopInThread enter ') loops = [] def start_background_loop() -> None: async def forever(): log('🚋6 log_loop') loops.append(asyncio.get_running_loop()) while True: await asyncio.sleep(100) log('🚋5 make asyncio loop') asyncio.run(forever()) log('🚋19 start_background_loop done') t = Thread(target=start_background_loop, daemon=True) t.start() while not loops: time.sleep(.1) loop = loops[0] log('🚋7 loop has started in thread') asyncio.run_coroutine_threadsafe(task, loop) log('🚋8 started task') log('🚋9 startLoopInThread exit') return loop