python多处理进程计数

时间:2014-09-02 09:27:38

标签: python multiprocessing

#coding:utf-8

import sys
import time
import os
import multiprocessing

class Worker(object):
    def __init__(self):
        self.progress = 0
        self.task_info = None

    def init(self):
        pass

    def status(self):
        pass

    def set_task_info(self, task_info):
        self.task_info = task_info

    def run(self, worker_status_meta_dict):
        print multiprocessing.current_process()
        print "process is %d" % self.progress
        while self.progress < 5:
            self.progress = self.progress +1
            worker_status_meta_dict['state'] = 0
            worker_status_meta_dict['status'] = "running"
            time.sleep(2)
        worker_status_meta_dict['state'] = 1
        worker_status_meta_dict['status'] = "succeeded"
        print "bavscan worker finished..."      

if __name__ == "__main__":
    worker = Worker()
    worker_process_dict = multiprocessing.Manager().dict()
    process = multiprocessing.Process(target=Worker.run, args=(worker, worker_process_dict))
    process.start()
    time.sleep(60)

这是python多进程的简单演示。 主进程使用multiprocessing.Process在子进程中调用Worker.run方法。 当在wondows 7中运行它时,主要过程将进行两个子过程。

enter image description

1 个答案:

答案 0 :(得分:0)

我在&#34; Python27 \ Lib \ multiprocessing__init __找到了问题.py&#34;

def Manager():
    '''
    Returns a manager associated with a running server process

    The managers methods such as `Lock()`, `Condition()` and `Queue()`
    can be used to create shared objects.
    '''
    from multiprocessing.managers import SyncManager
    m = SyncManager()
    m.start()
    return m

m.start()将启动一个子进程来启动管理器。