如何确保一个流程先于另一个流程启动



如何确保一个进程先于另一个进程启动?

背景:我在两个独立的进程中进行计算(每个进程都在循环中运行(。但对于一个过程中的计算,我需要另一个过程的计算结果。第一个想法可能是:

为什么不同步示例代码:

from multiprocessing import Process, Queue
import time
import random
queue = Queue()
def some_calculation():
# this is slower
time.sleep(2)
return random.randint(0, 10)
def some_other_calculation(required):
# this is faster
time.sleep(0.5)
return required + random.randint(0, 10)
def first_target(queue):
while True:
# doing some computations
res = some_calculation()
queue.put(res)
def second_target(queue):
res_list= []
# req = 5 as an initial guess might also work, but lets
# assume this is not an option
while True:
try:
req = queue.get(block=False)
except:
print('no new value available, reuse old value')
res = some_other_calculation(req)
res_list.append(res)
print('results so far:', res_list)
if __name__ == '__main__':
proc1 = Process(target=first_target, args=(queue,))
proc2 = Process(target=second_target, args=(queue,))
proc1.start()
# time.sleep(3) here everything works fine, but is there
# maybe a more elegant solution?
proc2.start()

除了休眠或预先定义req的值之外,有没有一种方法可以确保这一个过程真正在另一个过程之前开始?

非常感谢您提前提供的帮助!

second_target的想法是启动一个线程,该线程将在队列上发出阻塞的get调用,从而使用可用的最新值不断更新req变量。second_target还需要对some_calculation进行初始同步调用,以获得其初始值req,从而大大简化逻辑。为了演示的目的,我还将second_target中的无限while True循环替换为更有限的循环,以便程序终止:

from multiprocessing import Process, Queue
import time
import random
from threading import Thread

def some_calculation():
# this is slower
time.sleep(2)
return random.randint(0, 10)
def some_other_calculation(required):
# this is faster
time.sleep(0.5)
return required + random.randint(0, 10)
def first_target(queue):
while True:
# doing some computations
res = some_calculation()
queue.put(res)
def second_target(queue):
def monitor_queue():
nonlocal queue, req
while True:
req = queue.get()
res_list= []
req = some_calculation() # Call it synchronously for initial value
last_req = req
Thread(target=monitor_queue, daemon=True).start()
for _ in range(10): # for testing purposes so we terminate:
#while True:
if req == last_req:
print('no new value available, reuse old value')
else:
print('got a new value')
last_req = req
res = some_other_calculation(req)
res_list.append(res)
print('results so far:', res_list)
if __name__ == '__main__':
queue = Queue()
p = Process(target=first_target, args=(queue,), daemon=True)
p.start()
second_target(queue)

打印:

no new value available, reuse old value
results so far: [5]
got a new value
results so far: [5, 2]
no new value available, reuse old value
results so far: [5, 2, 2]
no new value available, reuse old value
results so far: [5, 2, 2, 7]
no new value available, reuse old value
results so far: [5, 2, 2, 7, 11]
got a new value
results so far: [5, 2, 2, 7, 11, 11]
no new value available, reuse old value
results so far: [5, 2, 2, 7, 11, 11, 13]
no new value available, reuse old value
results so far: [5, 2, 2, 7, 11, 11, 13, 13]
got a new value
results so far: [5, 2, 2, 7, 11, 11, 13, 13, 7]
no new value available, reuse old value
results so far: [5, 2, 2, 7, 11, 11, 13, 13, 7, 11]

相关内容

  • 没有找到相关文章

最新更新