asyncio后台线程:主线程阻塞中的运行功能

时间:2019-02-01 10:39:37

标签: python python-3.x multithreading python-asyncio

我有两个线程(主线程和一些后台线程),并且都有自己的asyncio事件循环。

现在考虑我在后台线程中,并且我想在主线程中执行某些操作(func_for_main_thread)。这样做是异步的:

main_thread_loop.call_soon_threadsafe(func_for_main_thread)

但是,如何执行同步/阻止操作,即等到func_for_main_thread执行完毕?

相关的是this问题,它向Qt询问相同的问题,并描述了Apple GCD的相同功能,基本上是:

dispatch_async(dispatch_get_main_queue(), ^{ /* do sth */ });

vs:

dispatch_sync(dispatch_get_main_queue(), ^{ /* do sth */ });

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您想要的内容,那么没有任何阻止您将Future传递到主线程以将其设置为func_for_main_thread完成后再进行设置。在后台线程中,您可以等待这个未来。

换句话说:

import asyncio
from functools import partial


async def called_threadsafe(loop, func):
    current_loop = asyncio.get_event_loop()
    fut = asyncio.Future()

    def call_and_set():
        try:
            res = func()
        except Exception as exc:
            f = partial(fut.set_exception, exc)
            current_loop.call_soon_threadsafe(f)
        else:
            f = partial(fut.set_result, res)
            current_loop.call_soon_threadsafe(f)
    loop.call_soon_threadsafe(call_and_set)  # submit to execute in other thread

    return await fut  # in current thread await other thread executed func and set future

完整代码演示其工作方式:

import asyncio
from functools import partial
import threading
import time


async def called_threadsafe(loop, func):
    current_loop = asyncio.get_event_loop()
    fut = asyncio.Future()

    def call_and_set():
        try:
            res = func()
        except Exception as exc:
            f = partial(fut.set_exception, exc)
            current_loop.call_soon_threadsafe(f)
        else:
            f = partial(fut.set_result, res)
            current_loop.call_soon_threadsafe(f)
    loop.call_soon_threadsafe(call_and_set)

    return await fut


# helpers:
_l = threading.Lock()

def info(*args):
    with _l:
        print(*args, threading.get_ident(), flush=True)


def start_bg_loop():
    bg_loop = asyncio.new_event_loop()

    def startup():
        asyncio.set_event_loop(bg_loop)
        bg_loop.run_forever()

    t = threading.Thread(target=startup)
    t.daemon = True
    t.start()

    return bg_loop


# main part:
def func_for_main_thread():
    info('executed in fg thread')
    time.sleep(0.05)
    return 'got result in bg thread'


async def bg_main(fg_loop):
    info('bg_main started')
    await asyncio.sleep(0.1)

    res = await called_threadsafe(fg_loop, func_for_main_thread)
    info(res)

    info('bg_main finished')


async def fg_main(bg_loop):
    info('fg_main started')
    await asyncio.sleep(1)
    info('fg_main finished')


fg_loop = asyncio.get_event_loop()
bg_loop = start_bg_loop()

asyncio.run_coroutine_threadsafe(bg_main(fg_loop), bg_loop)
fg_loop.run_until_complete(fg_main(bg_loop))

输出:

fg_main started 2252
bg_main started 5568
executed in fg thread 2252
got result in bg thread 5568
bg_main finished 5568
fg_main finished 2252