coconut
coconut copied to clipboard
Add an asyncio event loop based map
Should be able to support:
listen_for_requests() |> async_map$(handle_request)
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 Yes, I agree, I think that's the right way to implement this.
What type would listen_for_requests()
return? Is it just an iterator or is it some asyncio
type thing. Thanks!
@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?
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.
I remember that it was originally my suggestion, but now I fail to recall how exactly I had envisioned it implemented, sorry :-(
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
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.