coconut icon indicating copy to clipboard operation
coconut copied to clipboard

Add an asyncio event loop based map

Open evhub opened this issue 7 years ago • 6 comments

Should be able to support:

listen_for_requests() |> async_map$(handle_request)

evhub avatar Jul 10 '16 23:07 evhub

Don't you think it would make sense to pass an event loop explicitly? Like loop = asyncio.get_event_loop(); listen_for_requests() |> async_map$(loop, handle_request). This would allow to decouple from asyncio module (which has provisional status) and plug in an alternative loop, like uvloop.

moigagoo avatar Jul 11 '16 03:07 moigagoo

@moigagoo Yes, I agree, I think that's the right way to implement this.

evhub avatar Jul 11 '16 18:07 evhub

What type would listen_for_requests() return? Is it just an iterator or is it some asyncio type thing. Thanks!

Boscillator avatar Aug 24 '16 16:08 Boscillator

@Boscillator Good question! I would assume just an iterator, but I'm not familiar enough with asyncio to really know what the most idiomatic way to implement this would be—maybe it needs to be something else?

evhub avatar Aug 24 '16 17:08 evhub

It returns an asynchronous function, which in essence is a generator. The key difference between a regular generator and an async func is that the latter can pass control to the event loop with await keyword.

moigagoo avatar Aug 24 '16 17:08 moigagoo

I remember that it was originally my suggestion, but now I fail to recall how exactly I had envisioned it implemented, sorry :-(

moigagoo avatar Aug 24 '16 17:08 moigagoo

Some basic implementations of useful primitives here using PEP 525 syntax:

async def amap(f, aiter):
    async for x in aiter:
        yield f(x)

async def aconsume(aiter):
    async for x in aiter:
        pass

async def alist(aiter):
    l = []
    async for x in aiter:
        l.append(x)
    return l

evhub avatar Sep 23 '22 04:09 evhub

This has now been added on coconut-develop as a special case of fmap. If fmap is passed an asynchronous iterable, it will now implement the amap logic above to map over it asynchronously.

evhub avatar Sep 24 '22 02:09 evhub