Run function for all arguments using multiple processes.
(func, args, num_proc, max_batch_size=1)
| 30 | |
| 31 | # Parallel processes |
| 32 | def parallel_map(func, args, num_proc, max_batch_size=1): |
| 33 | """Run function for all arguments using multiple processes.""" |
| 34 | # De-activate/Restore any inner OpenCV threading |
| 35 | threads_used = cv2.getNumThreads() |
| 36 | cv2.setNumThreads(0) |
| 37 | |
| 38 | num_proc = min(num_proc, len(args)) |
| 39 | if num_proc <= 1: |
| 40 | res = list(map(func, args)) |
| 41 | else: |
| 42 | with parallel_backend('loky', n_jobs=num_proc): |
| 43 | batch_size = max(1, int(len(args) / (num_proc * 2))) |
| 44 | batch_size = min(batch_size, max_batch_size) if max_batch_size else batch_size |
| 45 | res = Parallel(batch_size=batch_size)(delayed(func)(arg) for arg in args) |
| 46 | |
| 47 | cv2.setNumThreads(threads_used) |
| 48 | return res |
| 49 | |
| 50 | |
| 51 | # Memory usage |
no test coverage detected