【问题标题】:how to query/setting variable in one python process form another python process?如何从另一个python进程查询/设置一个python进程中的变量?
【发布时间】:2015-10-05 04:44:50
【问题描述】:

Python sn-p A:

import time

class task:
    def __init__(self): 
        self.progress_percent = 0

    def working(self):
        self.progress_percent = self.progress_percent + 1    
task1 = task()
task2 = task()

while True:
    time.sleep(1)
    task1.working()
    task2.working()    
    print task1.progress_percent
    print task2.progress_percent

如何从另一个单独的python进程查询甚至设置task1.progress_percenttask2.progress_percent运行时? (不是 pdb)

谢谢。

【问题讨论】:

    标签: python process ipc


    【解决方案1】:

    您可以使用 multiprocessing.Process 和 Condition 来做到这一点。请尝试以下代码:

    import multiprocessing
    
    import time
    
    class Task(multiprocessing.Process):
        counter = 0
        name = None
        event = None
        another_tasks = set()
        def __init__(self, mgr_dict, cond): 
            super(Task, self).__init__()
            self.mgr_dict = mgr_dict
            self.cond = cond
            cls = self.__class__
            cls.counter += 1
            self.name = '{} {}'.format(cls.__name__, cls.counter)
            self.mgr_dict[self.name] = 0
    
        def value(self):
            return self.mgr_dict[self.name]
    
        def run(self):
            while True:
                with self.cond:
                    self.cond.wait()
                    self.mgr_dict[self.name] += 1
    
    class SubTask(Task):
        pass
    
    class MainTask(Task):
        subtasks = set()
        def working(self):
            with self.cond:
                self.cond.notify_all()
    
        def start(self):
            super(MainTask, self).start()
            for subtask in self.subtasks:
                subtask.start()
    
        def create_subtask(self):
            subtask = SubTask(self.mgr_dict, condition)
            self.subtasks.add(subtask)
    
        def join(self):
            self.mgr_dict[self.name]
    
        def shutdown(self):
            self.exit.set()
    
    event = multiprocessing.Event()
    if __name__ == '__main__':
        mgr = multiprocessing.Manager()
        mgr_dict = mgr.dict()
    
        condition = multiprocessing.Condition()
        task1 = MainTask(mgr_dict, condition)
        subtask1 = task1.create_subtask()
        subtask2 = task1.create_subtask()
        subtask3 = task1.create_subtask()
        subtask4 = task1.create_subtask()
        subtask5 = task1.create_subtask()
        task1.start()
        while task1.value() < 100:
            time.sleep(1)
            task1.working()
            print mgr_dict
    

    结果:

    {'MainTask 1': 0, 'SubTask 1': 0, 'SubTask 2': 0, 'SubTask 3': 0, 'SubTask 4': 0, 'SubTask 5': 0}
    {'MainTask 1': 1, 'SubTask 1': 1, 'SubTask 2': 1, 'SubTask 3': 1, 'SubTask 4': 1, 'SubTask 5': 1}
    .
    .
    .
    {'MainTask 1': 100, 'SubTask 1': 100, 'SubTask 2': 100, 'SubTask 3': 100, 'SubTask 4': 100, 'SubTask 5': 100}
    

    【讨论】:

    • 感谢您的回复,但是我想知道我是否可以从另一个单独的“python 进程”中查询 task.progress_percent,类似于进程之间的“IPC”
    • Ops.. 抱歉 =) 您可以使用共享命名空间来做到这一点。如果你愿意,我可以举个例子。
    • 如果你能展示一个例子,更多的,“共享命名空间”是否可以为不同的 python 进程工作而不是由进度 A 产生的?
    • 你应该有主进程。但是所有的进展都可以在 Process 内部。稍等片刻,我将完成示例 =)
    • 谢谢。而且我发现模块“rpyc”或“pyro”对于处理 RPC 和 IPC 访问问题很有用。
    猜你喜欢
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 2016-03-18
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    相关资源
    最近更新 更多