Python 多处理 Return 值处理

Python Multiprocessing Return Value Processing

我在如何对每个多处理进行 return 结果比较时遇到了问题。 我正在为我的功能做多处理。我的函数将 return 一个值。我想 运行 我的函数 5 次并比较哪个进程具有最低的 return 值。我的代码如下。

def do_processVal():
   getParamInit()
   do_evaluation()
   currbestVal = bestGlobalVal

   return 'Current best value: ', currbestVal, 'for process{}'.format(os.getpid())

from multiprocessing import Pool
import concurrent.futures
from os import getpid
import time
import os

start = time.perf_counter()

with concurrent.futures.ProcessPoolExecutor() as executor:
   results = [executor.submit(do_processVal) for _ in range(5)]

   for f in concurrent.futures.as_completed(results):
      print(f.results())

finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')

截至目前的输出:

Current best value: 12909.5 for process 21918
Current best value: 12091.5 for process 21920
Current best value: 12350.0 for process 21919
Current best value: 12000.5 for process 21921
Current best value: 11901.0 for process 21922
Finish in 85.86 second(s)

我要的是上面所有5个return值,我想取值最低的数据。在此示例中,进程 21922 具有最低值。所以我想给一个参数赋值。

FinalbestVal = 11901.0

如果我没记错的话,你可以在 do_processVal() 中简单地 return currbestVal 而不是字符串。

然后你就可以收集它们了 select min

(...)

values = []
for f in concurrent.futures.as_completed(results):
    values.append(f.results())
print(f"FinalbestVal = {min(values)}")

(...)

试试这个:

def do_processVal():
   getParamInit()
   do_evaluation()
   currbestVal = bestGlobalVal

   return currbestVal, 'for process{}'.format(os.getpid())

from multiprocessing import Pool
import concurrent.futures
from os import getpid
import time
import os

start = time.perf_counter()

with concurrent.futures.ProcessPoolExecutor() as executor:
   results = [executor.submit(do_processVal) for _ in range(5)]

   minimal_value = 1000000
   for f in concurrent.futures.as_completed(results):
      res, s = f.results()
      if res < minimal_value:
          minimal_value = res
      print('Current best value: '+ str(currbestVal) + s) # not sure what format s 
                                                             will have, you might need                         
                                                             to change that
print("minimal value:" + str(minimal_value))
finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')

据我所知,您混合了职能的职责,这导致了您的问题。 returns 下面的函数只有数据。收集数据后运行的代码对数据进行评估。然后数据会一次性呈现出来。分离代码的职责是几十年前清洁代码的关键。长期以来,它作为最佳实践得到支持的原因是因为像您 运行 这样的问题。它还使代码更容易在以后重用而无需更改。

def do_processVal():
   getParamInit()
   do_evaluation()
   currbestVal = bestGlobalVal
   return [currbestVal, os.getpid()]

from multiprocessing import Pool
import concurrent.futures
from os import getpid
import time
import os

start = time.perf_counter()

with concurrent.futures.ProcessPoolExecutor() as executor:
    results = [executor.submit(do_processVal) for _ in range(5)]

best_value = -1
values_list = []
for f in concurrent.futures.as_completed(results):
    values = f.result()
    values_list.append(values)
    if best_value == -1 or values[0] < best_value:
        best_value = values[0]

for i in values_list:
    print(f'Current best value: {i[0]} for process {i[1]}')

finish = time.perf_counter()
print(f'Finished in {round(finish-start, 2)} second(s)')
print(f'Final best = {best_value}')