[PYTHON]MultiProcess多进程使用

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, ))

说明:

  • python要使用多核心计算资源可以通过多进程来实现,一般进程数量和cpu核心数量相关;
  • python多进程对性能的提升有极限,如果对性能要求极高,可以尝试用python调用c接口的方式实现;

你可能感兴趣的:(PYTHON)