@@ -717,15 +717,25 @@ def __init__(
717717 self ,
718718 iterator : AsyncIterator [T ],
719719 transformation : Callable [[T ], Coroutine [Any , Any , U ]],
720+ concurrency : int ,
720721 buffersize : int ,
721722 ordered : bool ,
722723 ) -> None :
723724 super ().__init__ (iterator , buffersize , ordered )
724725 self .transformation = transformation
726+ self .concurrency = concurrency
727+ self .semaphore : asyncio .Semaphore
728+
729+ def _context_manager (self ) -> ContextManager :
730+ self .semaphore = asyncio .Semaphore (
731+ self .concurrency , loop = asyncio .get_running_loop ()
732+ )
733+ return super ()._context_manager ()
725734
726735 async def _safe_transformation (self , elem : T ) -> Union [U , ExceptionContainer ]:
727736 try :
728- return await self .transformation (elem )
737+ async with self .semaphore :
738+ return await self .transformation (elem )
729739 except Exception as e :
730740 return ExceptionContainer (e )
731741
@@ -749,13 +759,15 @@ def __init__(
749759 self ,
750760 iterator : AsyncIterator [T ],
751761 transformation : Callable [[T ], Coroutine [Any , Any , U ]],
762+ concurrency : int ,
752763 buffersize : int ,
753764 ordered : bool ,
754765 ) -> None :
755766 super ().__init__ (
756767 _ConcurrentAMapAsyncIterable (
757768 iterator ,
758769 transformation ,
770+ concurrency ,
759771 buffersize ,
760772 ordered ,
761773 ).__aiter__ ()
0 commit comments