聚合物
polymer的Python项目详细描述
摘要
并行运行任务的简单框架。类似于 多处理。池,但有一些改进。例如, pool只对多处理函数(而不是对象)有用。你可以 在对象周围包装函数,但只处理任务会更好 对象本身。
聚合物主要用于工人错误记录和运行时统计。 它还会自动重新启动崩溃的多处理工作进程(对于 多处理。池)。当工人撞车时,聚合物知道工人 正在执行并重新提交该任务。这绝对不是傻瓜的证据; 不过,这是一个有用的功能。
taskmgr().supervise()完成后,将返回一个对象实例列表。 可以将每个任务的结果存储为每个对象实例的属性。
用法
importtimefrompolymer.PolymerimportControllerQueue,TaskMgrfrompolymer.abc_taskimportBaseTaskclassSimpleTask(BaseTask):def__init__(self,text="",wait=0.0):super(SimpleTask,self).__init__()self.text=textself.wait=waitdefrun(self):"""run() is where all the work is done; this is called by TaskMgr()"""## WARNING... using try / except in run() could squash Polymer's## internal error logging...time.sleep(float(self.wait/10))printself.textdef__eq__(self,other):"""Define how tasks are uniquely identified"""ifother.text==self.text:returnTruereturnFalsedef__repr__(self):return"""<{0}, wait: {1}>""".format(self.text,self.wait)defController():"""Controller() builds a list of tasks, and queues them to the TaskMgr There is nothing special about the name Controller()... it's just some code to build a list of SimpleTask() instances."""tasks=list()## Build ten tasks... do *not* depend on execution order...num_tasks=10foriiinrange(0,num_tasks):tasks.append(SimpleTask(text="Task {0}".format(ii),wait=ii))targs={'work_todo':tasks,# a list of SimpleTask() instances'hot_loop':False,# If True, continuously loop over the tasks'worker_count':3,# Number of workers (default: 5)'resubmit_on_error':False,# Do not retry errored jobs...'queue':ControllerQueue(),'worker_cycle_sleep':0.001,# Worker sleep time after a task'log_stdout':False,# Don't log to stdout (default: True)'log_path':"taskmgr.log",# Log file name'log_level':0,# Logging off is 0 (debugging=3)'log_interval':10,# Statistics logging interval}## task_mgr reads and executes the queued taskstask_mgr=TaskMgr(**targs)## a set() of completed task objects are returned after supervise()results=task_mgr.supervise()returnresultsif__name__=='__main__':Controller()
许可证
gplv3