util/lib/analysis_package/code_template/concurrency/task_distribution.py

29 lines
890 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project IoD_data_analysis_tool
@File distribute_task.py
@IDE PyCharm
@Author rengengchen
@Time 2022/8/8 16:55
"""
import math
import multiprocessing
def equally_distributing_task(target, tasks, *args, results=None, num_processors=8):
len_tasks = len(tasks)
process_offset = math.ceil(len_tasks / num_processors)
for i in range(num_processors):
sub_tasks = tasks[i * process_offset: (i + 1) * process_offset]
if sub_tasks:
if results:
multiprocessing.Process(target=target,
args=(sub_tasks, results, *args)).start()
else:
multiprocessing.Process(target=target,
args=(sub_tasks, *args)).start()
else:
break
return results