view src/light9/blender/asyncio_thread.py @ 2432:b8a408caf115

start blender sync
author drewp@bigasterisk.com
date Tue, 28 May 2024 15:35:12 -0700
parents
children
line wrap: on
line source

import asyncio
from threading import Thread
from typing import Coroutine


def startLoopInThread(task: Coroutine) -> asyncio.AbstractEventLoop:

    def start_background_loop(loop: asyncio.AbstractEventLoop) -> None:
        asyncio.set_event_loop(loop)
        loop.run_forever()

    loop = asyncio.new_event_loop()
    t = Thread(target=start_background_loop, args=(loop,), daemon=True)
    t.start()
    asyncio.run_coroutine_threadsafe(task, loop)
    return loop