29 lines
890 B
Python
29 lines
890 B
Python
#!/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
|