MCPcopy Create free account
hub / github.com/Pints-AI/1.5-Pints / bounded_parallel_map

Function bounded_parallel_map

tokenizer/convert/convert.py:902–934  ·  view source on GitHub ↗

Parallel map, but with backpressure. If the caller doesn't call `next` fast enough, this will stop calling `func` at some point rather than letting results pile up in memory. Specifically, there is a max of one output value buffered per thread.

(func: Callable[[In], Out], iterable: Iterable[In], concurrency: int, max_workers: int | None = None, use_processpool_executor: bool = False)

Source from the content-addressed store, hash-verified

900
901
902def bounded_parallel_map(func: Callable[[In], Out], iterable: Iterable[In], concurrency: int, max_workers: int | None = None, use_processpool_executor: bool = False) -> Iterable[Out]:
903 '''Parallel map, but with backpressure. If the caller doesn't call `next`
904 fast enough, this will stop calling `func` at some point rather than
905 letting results pile up in memory. Specifically, there is a max of one
906 output value buffered per thread.'''
907 if concurrency < 2:
908 yield from map(func, iterable)
909 # Not reached.
910 iterable = iter(iterable)
911 executor_class: type[ThreadPoolExecutor] | type[ProcessPoolExecutor]
912 if use_processpool_executor:
913 executor_class = ProcessPoolExecutor
914 else:
915 executor_class = ThreadPoolExecutor
916 with executor_class(max_workers=max_workers) as executor:
917 futures: list[concurrent.futures.Future[Out]] = []
918 done = False
919 for _ in range(concurrency):
920 try:
921 futures.append(executor.submit(func, next(iterable)))
922 except StopIteration:
923 done = True
924 break
925
926 while futures:
927 result = futures.pop(0).result()
928 while not done and len(futures) < concurrency:
929 try:
930 futures.append(executor.submit(func, next(iterable)))
931 except StopIteration:
932 done = True
933 break
934 yield result
935
936
937def check_vocab_size(params: Params, vocab: Vocab, pad_vocab: bool = False) -> None:

Callers 1

write_allMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected