我有一個庫,它提供了一個選項,可以在外部作業完成時安排回調。 Future.set_result()
使用此回調安全嗎?如果沒有,那麼實現這一目標的正確方法是什麼?未來的文檔說它的方法不是線程安全的,所以我認爲這可能有問題。在來自不同線程的回調中設置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()