对于并行编程,总是有不止一种方法可以做事情。所以其他人可能对如何做到这一点有完全不同的想法。在
首先想到的方法是通过^{}。保持其中三个,并根据需要打开/关闭它们。在from threading import Thread, Event
def worker1(events):
a,b,c = events
while True:
a.wait() # sleep here if 'a' event is set, otherwise continue
# do work here
if some_condition:
c.clear() # put c to sleep
b.set() # wake up, b
def worker2(events):
a,b,c = events
while True:
b.wait()
#do work
if some_condition:
a.clear()
c.set()
def worker3(events):
a,b,c = events
while True:
c.wait()
#do work
if some_condition:
b.clear()
a.set()
然后启动它们:
^{pr2}$
粗糙的未经测试的代码,而且比需要的更详细(您可以使用一个worker def编写这些代码,它需要一些额外的参数),但希望能让您走上正确的道路。在