Skip to content

Support for async iterators in seq functions #1181

Closed
@ikappaki

Description

@ikappaki

Hi,

Python provides async for to iterate over async iterators.

The documentation at the yield special form suggests that "...Basilisp seq and sequence functions integrate seamlessly with Python generators", but it seems that doesn't cover async generators.

For example, given the following python file which defines an agen async generator function, and demonstrate its use:
afor.py

import asyncio

async def agen():
    for i in range(5):
        await asyncio.sleep(0.01)
        yield i

async def main():
    async for value in agen():
        print(value)

if __name__ == "__main__":
    asyncio.run(main())

when run, it successfully iterates over agen

> python afor.py
0
1
2
3
4

However, If i try to do the same over the same async generator agen from Basilisp, using a seq (or vec), it fails with an async_generator object is not iterable error:
issuefor.lpy

(import asyncio afor)

(defasync main
  []
  (seq (afor/agen)))

(asyncio/run (main))
> basilisp run issuefor.lpy
Traceback (most recent call last):
...
  File "C:\src\basilisp\src\basilisp\lang\seq.py", line 254, in to_seq
    return _seq_or_nil(sequence(o))
                       ^^^^^^^^^^^
  File "C:\src\basilisp\src\basilisp\lang\seq.py", line 223, in sequence
    i = iter(s)
        ^^^^^^^
TypeError: 'async_generator' object is not iterable

I'm not sure what the right solution is here. Would it be possible to extend the "seq and sequence functions" as described in the documentation to support async iterators?

Thanks

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions