Consider some library with an interface like this:
RemoteTask.start()
RemoteTask.cancel()
RemoteTask.get_id()
RemoteTask.get_result()
RemoteTask.is_done()
For example, concurrent.futures.Future
implements an API like this, but I don't want to assume the presence of a function like concurrent.futures.wait
.
In traditional Python code, you might need to poll for results:
def foo():
task = RemoteTask()
while not task.is_done():
time.sleep(2)
return task.get_result()
Is there some general recommended best-practice technique for wrapping this in an Awaitable
interface?
The desired usage would be:
async def foo():
task = RemoteTask()
return await run_remote_task()
I understand that the implementation details might differ across async libraries, so I am open to both general strategies for solving this problem, and specific solutions for Asyncio, Trio, AnyIO, or even Curio.
Assume that this library cannot be easily modified, and must be wrapped.