如何进行进程间通信?
How to do inter-process communication?
我们正在 Python3 在 Raspberry Pi3 上进行多处理。我们在流程中做口罩检测、人数统计和咳嗽检测。我们现在有 3 个过程。所有这些都被描述为函数。 (def mask ()
, .. def cough ()
等)都是同时工作的,这里没有问题。问题是作为第4个进程,我们要根据这3个进程的信息写一个告警函数
我们的目标是如果在这 3 次检测中出现不希望的情况(例如错误使用口罩,人数太多),则在屏幕上打印错误。 4、作为一个进程,我们不能在alarm ()
函数下用if-else命令使用其他函数返回的值。我写了下面代码的那种总结。我们如何在流程中使用数据?现在,alarm()
无法正常工作。
def mask():
while True:
if problem:
return True
def cough():
while True:
if problem:
return True
def alarm():
while True:
if mask():
print("Eror1")
elif cough():
print("Error2")
if __name__ == "__main__":
p1 = multiprocessing.Process(target=cough,)
p2 = multiprocessing.Process(target=mask,)
p3 = multiprocessing.Process(target=alarm,)
p1.start()
p2.start()
p3.start()
看看Python docs on multiprocessing, especially the parts about exchanging objects between processes and sharing state between processes。
如果您只想在某些情况发生时向主进程发出信号,Event
object should probably be enough. If you want to exchange more complex data between the processes, a Queue
会更适合。
使用 Event
对象,您的代码大致如下所示:
import multiprocessing
from random import random
import time
def mask(event):
while True:
if random() < 0.1:
event.set()
time.sleep(1)
def cough(event):
while True:
if random() < 0.2:
event.set()
time.sleep(1)
def alarm(events):
while True:
for i, event in enumerate(events, start=1):
# wait for up to a second for the event to be set by the other process
# and then wait for the next event
if event.wait(1):
print('Error ' + str(i))
event.clear()
def main():
e1 = multiprocessing.Event()
p1 = multiprocessing.Process(target=mask, args=(e1,))
p1.start()
e2 = multiprocessing.Event()
p2 = multiprocessing.Process(target=cough, args=(e2,))
p2.start()
alarm([e1, e2])
if __name__ == '__main__':
main()
我们正在 Python3 在 Raspberry Pi3 上进行多处理。我们在流程中做口罩检测、人数统计和咳嗽检测。我们现在有 3 个过程。所有这些都被描述为函数。 (def mask ()
, .. def cough ()
等)都是同时工作的,这里没有问题。问题是作为第4个进程,我们要根据这3个进程的信息写一个告警函数
我们的目标是如果在这 3 次检测中出现不希望的情况(例如错误使用口罩,人数太多),则在屏幕上打印错误。 4、作为一个进程,我们不能在alarm ()
函数下用if-else命令使用其他函数返回的值。我写了下面代码的那种总结。我们如何在流程中使用数据?现在,alarm()
无法正常工作。
def mask():
while True:
if problem:
return True
def cough():
while True:
if problem:
return True
def alarm():
while True:
if mask():
print("Eror1")
elif cough():
print("Error2")
if __name__ == "__main__":
p1 = multiprocessing.Process(target=cough,)
p2 = multiprocessing.Process(target=mask,)
p3 = multiprocessing.Process(target=alarm,)
p1.start()
p2.start()
p3.start()
看看Python docs on multiprocessing, especially the parts about exchanging objects between processes and sharing state between processes。
如果您只想在某些情况发生时向主进程发出信号,Event
object should probably be enough. If you want to exchange more complex data between the processes, a Queue
会更适合。
使用 Event
对象,您的代码大致如下所示:
import multiprocessing
from random import random
import time
def mask(event):
while True:
if random() < 0.1:
event.set()
time.sleep(1)
def cough(event):
while True:
if random() < 0.2:
event.set()
time.sleep(1)
def alarm(events):
while True:
for i, event in enumerate(events, start=1):
# wait for up to a second for the event to be set by the other process
# and then wait for the next event
if event.wait(1):
print('Error ' + str(i))
event.clear()
def main():
e1 = multiprocessing.Event()
p1 = multiprocessing.Process(target=mask, args=(e1,))
p1.start()
e2 = multiprocessing.Event()
p2 = multiprocessing.Process(target=cough, args=(e2,))
p2.start()
alarm([e1, e2])
if __name__ == '__main__':
main()