def parent(path, pause=False, fork=True): caoe.install(fork=fork) open(os.path.join(path, 'parent-%d' % os.getpid()), 'w').close() for i in range(3): p = Process(target=child, args=(path,)) p.daemon = True p.start() if pause: time.sleep(10) else: time.sleep(0.1)
#coding=utf-8 '''Workers处理任务''' # 因为比较简单的项目,就都在一起了,没有拆分 import multiprocessing # https://github.com/douban/CaoE, 父进程死掉杀掉子进程 import caoe caoe.install() import parse import models from schedulers import Message from log import error, warn from control import Scheduler, periodic, run terminating = None scheduler = Scheduler('worker') # Fixed 不能CTRL-C http://stackoverflow.com/questions/14579474/multiprocessing-pool-spawning-new-childern-after-terminate-on-linux-python2-7 def initializer(terminating_): # This places terminating in the global namespace of the worker subprocesses. # This allows the worker function to access `terminating` even though it is # not passed as an argument to the function. global terminating terminating = terminating_ class Worker(object): '''执行任务类''' def __init__(self, map_func, num_workers=None, **kwargs): self.map_func = map_func self.inputs = Message.objects(state__ne=2, inprocess__ne=True)