MCPcopy Create free account
hub / github.com/CUT3R/CUT3R / parallel_threads

Function parallel_threads

datasets_preprocess/utils/parallel.py:12–70  ·  view source on GitHub ↗

tqdm but with parallel execution. Will essentially return res = [ function(arg) # default function(*arg) # if star_args is True function(**arg) # if kw_args is True for arg in args] Note: the first elements of args will no

(
    function,
    args,
    workers=0,
    star_args=False,
    kw_args=False,
    front_num=1,
    Pool=ThreadPool,
    **tqdm_kw
)

Source from the content-addressed store, hash-verified

10
11
12def parallel_threads(
13 function,
14 args,
15 workers=0,
16 star_args=False,
17 kw_args=False,
18 front_num=1,
19 Pool=ThreadPool,
20 **tqdm_kw
21):
22 """tqdm but with parallel execution.
23
24 Will essentially return
25 res = [ function(arg) # default
26 function(*arg) # if star_args is True
27 function(**arg) # if kw_args is True
28 for arg in args]
29
30 Note:
31 the <front_num> first elements of args will not be parallelized.
32 This can be useful for debugging.
33 """
34 while workers <= 0:
35 workers += cpu_count()
36 if workers == 1:
37 front_num = float("inf")
38
39 # convert into an iterable
40 try:
41 n_args_parallel = len(args) - front_num
42 except TypeError:
43 n_args_parallel = None
44 args = iter(args)
45
46 # sequential execution first
47 front = []
48 while len(front) < front_num:
49 try:
50 a = next(args)
51 except StopIteration:
52 return front # end of the iterable
53 front.append(
54 function(*a) if star_args else function(**a) if kw_args else function(a)
55 )
56
57 # then parallel execution
58 out = []
59 with Pool(workers) as pool:
60 # Pass the elements of args into function
61 if star_args:
62 futures = pool.imap(starcall, [(function, a) for a in args])
63 elif kw_args:
64 futures = pool.imap(starstarcall, [(function, a) for a in args])
65 else:
66 futures = pool.imap(function, args)
67 # Print out the progress as tasks complete
68 for f in tqdm(futures, total=n_args_parallel, **tqdm_kw):
69 out.append(f)

Callers 3

mainFunction · 0.90
mainFunction · 0.90
parallel_processesFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected