当True循环在迭代时,我想发出一个信号,用整数,字典或字符串之类的数据更新等待它的Child.process
。在PyQt中
可以使用PyQt.Signal
对象来完成。 Qt信号对象可以设置为字符串,字典或列表。使用signal.emit
方法可以“发出”一个可能被另一个对象“捕获”的变量。如何在不使用PyQt信号对象的情况下使函数process
与孩子和/或父母互动?
import time, multiprocessing
def process():
num = int()
while True:
print '...sleeping %s' % num
time.sleep(1)
num += 1
if num > 10:
break
return time.time()
class Child(object):
def __init__(self):
super(Child, self).__init__()
def process(self):
proc = multiprocessing.Process(target=process)
proc.start()
class Parent(object):
def __init__(self):
super(Parent, self).__init__()
child = Child()
time_completed = child.process()
print 'time_completed: %s' % time_completed
obj = Parent()
答案 0 :(得分:0)
使用multiprocessing.Queue
对象使process
函数与child
,然后与parent
对象进行通信:
import time, multiprocessing
def process(queue):
num = int()
while True:
queue.put('...sleeping %s' % num)
time.sleep(1)
num += 1
if num > 5:
break
queue.put('...time completed %s' % time.time() )
time.sleep(0.1)
class Child(object):
def __init__(self):
super(Child, self).__init__()
self.queue = multiprocessing.Queue()
self.proc = multiprocessing.Process(target=process, args=(self.queue,))
def process(self):
self.proc.start()
class Parent(object):
def __init__(self):
super(Parent, self).__init__()
children = [Child() for i in range(3)]
for child in children:
child.process()
for child in children:
if child.proc.is_alive():
if not child.queue.empty():
print child.queue.get()
obj = Parent()