While True loop is iterating I would like to emit a signal to update a waiting for it Child.process
with a data such as an integer, dictionary or a string. In PyQt it could be done using PyQt.Signal
object. Qt signal object can be set to be a string, dict or a list. The using signal.emit
method it would be possible to "emit" a variable that could be "captured" by another object. How to make the function process
to interact with a child and/or a parent without using PyQt signal object?
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()
Use multiprocessing.Queue
object to make the process
function communicate with the child
and then with the parent
object:
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.