import asyncio from typing import Any, List, Optional def chunker( limit: int ) -> Any: async def process( q_in: asyncio.Queue[ Any ], q_out: asyncio.Queue[ Any ] ) -> None: s = '' while True: item = await q_in.get() print( "{}, retrieved {}".format( limit, item ) ) if item is None: while s: if len( s ) <= limit: await q_out.put( str( s ) ) s = '' else: await q_out.put( str( s[:limit] ) ) s = s[ limit : ] break s += item if len( s ) < limit: continue if len( s ) <= limit: await q_out.put( str( s ) ) s = '' continue else: await q_out.put( str( s[ :limit ] ) ) s = s[ limit : ] continue await q_out.put( s ) await q_out.put( None ) return process import run_tests