MultiProcess多进程使用:
直接import和调用python的multiprocess库(分别使用Pool和Process来启动和管理多进程);
方法:
1.类库:
import abc
import multiprocessing
import queue
class IExecutor(object):
def __init__(self):
pass
@abc.abstractmethod
def init(self, info):
pass
@abc.abstractmethod
def execute_task(self, func, args):
""" 执行任务 """
pass
@abc.abstractmethod
def execute_multi_task(self, func, args):
""" 执行任务 """
pass
@abc.abstractmethod
def join(self):
pass
class ProcessPool(IExecutor):
""" Processes Pool """
def __init__(self):
IExecutor.__init__(self)
self.__pool = None
def init(self):
if self.__pool:
self.join()
self.__pool = multiprocessing.Pool()
def execute_task(self, func, args):
return self.__pool.apply(func = func, args = args)
def join(self):
self.__pool.close()
self.__pool.join()
def __getstate__(self):
self_dict = self.__dict__.copy()
del self_dict['__pool']
return self_dict
def __setstate__(self, state):
self.__dict__.update(state)
class MultiProcesses(IExecutor):
""" Processes """
def __init__(self):
IExecutor.__init__(self)
self.__processes = []
self.__count = min(multiprocessing.cpu_count(), MAX_PROCESSOR_NUM)
def init(self):
if len(self.__processes):
self.join()
def execute_multi_task(self, func, args):
for i in range(self.__count):
t = multiprocessing.Process(target = func, args = args)
t.start()
self.__processes.append(t)
def execute_task(self, func, args):
t = multiprocessing.Process(target = func, args = args)
t.start()
self.__processes.append(t)
def join(self):
for t in self.__processes:
t.join()
self.__processes = []
2.使用
executor = MultiProcesses()
executor.init()
executor.execute_multi_task(func, (arg1, arg2, arg3, ))
说明: