1

외부 작업이 완료 될 때 콜백을 예약하는 옵션을 제공하는 라이브러리가 있습니다. Future.set_result()에이 콜백을 사용해도 안전합니까? 그렇지 않다면 이것을 달성하기위한 적절한 방법은 무엇입니까? Future의 문서에 따르면이 메서드는 스레드로부터 안전하지 않으므로이 문제가있을 수 있다고 가정합니다.다른 스레드의 콜백에서 asyncio.Future의 값을 설정하십시오.

내 목표는 PyOpenCL의 OpenCL 이벤트를 asyncio 코드에서 기다릴 수있는 객체로 사용하는 것입니다. 나는이 같은 도우미 함수에 대해 생각했다 :

def wrap_opencl_event(event): 
    f = asyncio.Future() 
    event.set_callback(pyopencl.command_execution_status.COMPLETE, lambda x: f.set_result(None)) 
    return f 

과이 방법 사용 : 조금 더 철저하게 미래의 값이 콜백에서 설정해야합니다 보인다 설명서를 읽은 후

async def do_slow_stuff(): 
    ev1 = pyopencl.enqueue_something() 
    ev2 = pyopencl.enqueue_something_else(wait_for=[ev1]) 
    await wrap_opencl_event(ev2) 
    process_results() 

답변

1

을 이벤트 루프와 일정 :

def wrap_opencl_event(event): 
    loop = asyncio.get_event_loop() 
    f = loop.create_future() 
    event.set_callback(pyopencl.command_execution_status.COMPLETE, 
         lambda status: loop.call_soon_threadsafe(f.set_result, None)) 
    return f 
0

또 다른 옵션은 concurrent.futures을 사용하고 미래

을 래핑하는 것입니다
import asyncio.futures, concurrent.futures 
async def func_returning_future(loop): 
    fut = concurrent.futures.Future() 
    # Pass off to your other thread somehow 
    # and return a wrapped future for asyncio 
    return asyncio.futures.wrap_future(fut, loop = loop) 

또는당신이

await asyncio.futures.wrap_future(fut, loop = loop) 
과 마지막 줄을 교체, 루프 내에서 기다리고도 수는 def 함수는 비동기이기 때문이다