Home > Blockchain >  TypeError when awaiting dask futures
TypeError when awaiting dask futures

Time:01-20

I get the following error: TypeError: object list can't be used in 'await' expression

when I try to await futures as dask_client.gather(futures) or await futures.

I am using a Dask Client with asynchronous=True

Tried referring to the official docs https://distributed.dask.org/en/stable/asynchronous.html

CodePudding user response:

The error "TypeError: object list can't be used in 'await' expression" is occurring because you are trying to use the await keyword on a Python list, which is not an asynchronous object.

When using the Dask Client with the asynchronous=True option, the gather method returns a list of Futures, not an awaitable object.

Here's what you can do to solve this issue:

Instead of awaiting the list of futures, you can use the dask.async.compute function to convert the list of futures to an awaitable object.

await dask.async.compute(*futures)

You can also use the dask.compute function to convert the list of futures to a list of results

results = dask.compute(*futures)

Another option is to use the concurrent.futures.as_completed function which returns an iterator that yields the futures as they complete.

for future in concurrent.futures.as_completed(futures):
    result = await future
    # process the result

It's important to note that when using dask with asyncio, it's recommended to use the dask.compute or dask.async.compute functions rather than the dask_client.gather function.

Also, make sure you are using the latest version of Dask as this error might have been resolved in recent version.

CodePudding user response:

Adding asynchronous=True while initialising the dask client is not enough when you want to gather or wait for all futures to finish executing. asynchronous=True argument has to be passed to the gather method as well.

await dask_client.gather(futures, asynchronous=True)

  • Related