4

I have the following code

async def foo():
   some_tuple = tuple(map(bar, some_tuple))
async def bar(data):
   # Await another function and do some other stuff
   return something

Since bar is async it must be awaited. However, I am unsure where to await bar. I tried awaiting it inside map, I tried awaiting map, I tried awaiting tuple and nothing worked.

How do I await bar inside map?

SarmaEater
  • 53
  • 1
  • 4
  • Possible duplicate of https://stackoverflow.com/questions/36093838/how-can-i-asynchronously-map-filter-an-asynchronous-iterable/39125333 – Belegnar Jul 11 '20 at 08:58
  • 1
    @Belegnar The question is related to, but not really a duplicate of the one you pointed. In the older question the poster was aware how to write an async version of `map`, but was faced with the lack of async generator in then's Python. The answers concentrated on (now obsolete) ways to implement the async iterator without an async generator, which is not what the OP here needs. – user4815162342 Jul 11 '20 at 10:37

1 Answers1

8

You can't use map with async functions since map is synchronous and doesn't suspend execution inside the loop. The same applies to the tuple constructor which expects a synchronous generator.

Ti fix this, you can replace the map/tuple pair with a list comprehension, which can be async, and readily converted to tuple:

some_tuple = tuple([await fn(elem) for elem in iterable])

Another option is to use the async versions of map and tuple from the asyncstdlib package, which also provides a lot of additional and useful functionality:

import asyncstdlib.builtins.map as amap
import asyncstdlib.builtins.tuple as atuple

some_tuple = await atuple(amap(fn, iterable))
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • I tried all 3 methods, I get `TypeError: 'async_generator' object is not iterable` for each of them. Sorry, I am new to async in Python. – SarmaEater Jul 11 '20 at 10:01
  • @SarmaEater Yeah, the problem is that `tuple` is non-async as well. I've now amended the answer. – user4815162342 Jul 11 '20 at 10:08