Python snippet 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
How to query or even setting task1.progress_percent or task2.progress_percent runtime from another individual python process? (not pdb)
Thank you.
1 Answer 1
You can use multiprocessing.Process and Condition to do that. Please, try this code:
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
The result:
{'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}
Sign up to request clarification or add additional context in comments.
6 Comments
K. C
thanks for the reply, however what I want to know whether I can query task.progress_percent from another individual "python process", sort of "IPC" between processes
arkadyzalko
Ops.. Sorry =) You can do that with Shared Namespaces. I can make a example if you want.
K. C
appreciated if you can show an example, more, can 'Shared Namespaces' working for the different python process not spawned by progress A?
arkadyzalko
You should have main process. But all progress can be inside Process. Just a moment, I'm going to finish the example =)
K. C
thank you. And I find module 'rpyc' or 'pyro' can be useful to handle both RPC and IPC access problem.
|
lang-py