#!/usr/bin/env python3 # -*- coding: gbk -*- # @Time :2019/12/22 20:45 import os import multiprocessing from multiprocessing import Manager, Pool from multiprocessing.managers import BaseManager, BaseProxy class CodeData: function_data = () class DataManager(BaseManager): pass class DataProcess: def set_proxy(self, func_proxy, task): self.func_proxy = func_proxy self.task = task def sub_process(self): for func in self.func_proxy: print("task:%s,pid:%s, func:%s\n" % (self.task, os.getpid(), func)) class GeneratorProxy(BaseProxy): _exposed_ = ['__iter__', '__next__'] def __iter__(self): return self def __next__(self): return self._callmethod('__next__') def get_function_data_proxy(data): for func in data: yield func def init_data(): for i in range(1000): CodeData.function_data += (i,) def multi_process(): init_data() pool = Pool(4, maxtasksperchild=1) task_num = 6 DataManager.register('get_function_data_proxy', get_function_data_proxy, proxytype=GeneratorProxy) proxy_list = [] with DataManager() as manager: for task in range(task_num): p = manager.get_function_data_proxy(CodeData.function_data) proxy_list.append(p) for task in range(task_num): dp = DataProcess() dp.set_proxy(proxy_list[task], task) pool.apply_async(dp.sub_process) pool.close() pool.join() if __name__ == '__main__': multiprocessing.freeze_support() multi_process()
Python多进程任务创建自定义代理
最新推荐文章于 2024-05-27 14:37:20 发布