多处理同步

让我们假设我需要并行运行5个进程,但是进程2到5取决于进程1。如何确保进程1在其他进程之前运行?我应该同时使用Python的Multiprocessing Event()或Lock()还是同时使用两者?

示例1:

process 1
process 2 or 3 or 4 or 5
process 2 or 3 or 4 or 5
process 2 or 3 or 4 or 5
process 2 or 3 or 4 or 5

示例2:

process 3
process 1 or 2 or 4 or 5
process 1 or 2 or 4 or 5
process 1 or 2 or 4 or 5
process 1 or 2 or 4 or 5

Example3具有2个依赖项:

process 1
process 2 or 3 (run in parallel after 1)
process 4
process 5 or 6 (run in parallel after 1 and after 4)

所有进程都调用相同的函数(msg),但都返回不同的值。

我需要一些指导,不一定要编码,如果可以的话,谢谢。

伪代码:

import Multiprocessing as mp

function(msg):
    return 1 if msg == "one"
    return 2 if msg == "two"
    return 3 if msg == "three"
    return 4 if msg == "four"
    return 5 if msg == "five"

msgs = ['one','two','three','four','five']

jobs = []
for msg in msgs:
    p = Process(target=function,args=(msg,))
    p.start()
    jobs.append(p)

for job in jobs:
    job.join()

在这种情况下,所有进程将无序运行。

如果我要先执行进程1,就可以这样做:

可能的解决方案:

import Multiprocessing as mp

function(msg):
    return 1 if msg == "one"
    return 2 if msg == "two"
    return 3 if msg == "three"
    return 4 if msg == "four"
    return 5 if msg == "five"

msg = ['one']
p1 = Process(target=function,))
p1.start()
p1.join()


msgs = ['two',))
    p.start()
    jobs.append(p)

for job in jobs:
    job.join()

是否有更好的解决方案?它可以工作,但这并不意味着它不能以更好的方式完成(例如,更少的代码重复)。

happyboy_1 回答:多处理同步

不确定最后做什么,但是您可以为此目的使用Event来>

import multiprocessing as mp

def function(msg,events):
  if msg == "one":
    print(1)
    events[0].set()
  if msg == "two":
    print("2 waiting")
    events[0].wait()
    events[1].wait()
    print("2 done")
  if msg == "three":
    print(3)
    events[1].set()
  if msg == "four":
    print(4)
  if msg == "five":
    print("5 waiting")
    events[0].wait()
    print("5 done")

if __name__ == '__main__':
  events = [mp.Event(),mp.Event()]
  jobs = []
  for item in ['one','two','three','four','five']:
    job = mp.Process(target=function,args=(item,events))
    job.start()
    jobs.append(job)
  for job in jobs:
    job.join()

在这里,我故意介绍了第二个依赖项:p2同时依赖于p1和p3(而p5仍然依赖于p1)。这样,如果您多次运行它,它会显示出更多的变化(比起单个依赖项而言):

python procy.py
2 waiting
4
1
5 waiting
5 done
3
2 done

python procy.py
1
5 waiting
2 waiting
4
5 done
3
2 done

python procy.py
1
4
3
5 waiting
5 done
2 waiting
2 done
本文链接:https://www.f2er.com/3149332.html

大家都在问