使用concurrent.futures.ProcessPoolExecutor

时间:2017-10-16 16:17:40

标签: python error-handling

我一直在尝试使用concurrent.futures.ProcessPoolExecutor来获取程序,以便在需要时引发异常。由于某些未知原因,当我运行我的代码时,程序不会引发异常。这是我的代码(引用我已经构建的其他类):

import pickle
import concurrent.futures
from BoxesSimulation import SimulationResults, QuenchedGridTrailBiasSimulation
from BoxAnalysis import BoxAnalysis, DistributionResults
import misc


def sim_func(tilescale):
    number_of_iterations = 75
    loadloc = 'middle'
    pickle_file_name = 'Pickle Files/ExperimentalBoxDistribution' + loadloc + '.pickle'
    cube_densities = [100, 150, 200, 225, 250, 275, 300, 400]
    bias_values = ('hooke', 0.8, 10, 0.3)
    for cube_density in cube_densities:
        for seed in misc.seed_from_txt_files(cube_density):
            for iteration in range(1, number_of_iterations+1):
                print('scale = ' + str(tilescale) + ',seed = ' + seed + ',iter = '
                      + str(iteration))

                sim = QuenchedGridTrailBiasSimulation(pickle_file_name, bias_values,
                                                      seed=int(seed), num_stones=cube_density,
                                                      tile_scale=tilescale)

                load_path, load_trajectory_length, load_time, where_out = sim.run_simulation()
                try:
                    ResultsObject.append(SimulationResults(load_path, load_trajectory_length,
                                                           load_time, where_out, tilescale,
                                                           cube_density, seed=int(seed)))
                except NameError as e:
                    if e.args[0][5:16] == 'ResultsObject':
                        ResultsObject = SimulationResults(load_path, load_trajectory_length,
                                                          load_time, where_out, tilescale,
                                                          cube_density, seed=int(seed))
                    else:
                        raise
                except AttributeError:
                    ResultsObject = SimulationResults(load_path, load_trajectory_length,
                                                      load_time, where_out, tilescale,
                                                      cube_density, seed=int(seed))
    results_pickle_file_name = 'Pickle ' \
                               'Files/QuenchedMidBiasSimulationResults_Simulated_Cubes_Scale_' \
                               + str(tilescale) + '_iterations_' + str(number_of_iterations) \
                               + '.pickle'
    with open(results_pickle_file_name, 'wb') as handle:
        pickle.dump([ResultsObject, tilescale, cube_densities, number_of_iterations,
                     bias_values], handle,
                    protocol=pickle.HIGHEST_PROTOCOL)


def main_func():
    scale_list = [2, 4, 6, 8, 10, 15]
    with concurrent.futures.ProcessPoolExecutor(max_workers=3) as executor:
        executor.map(sim_func, scale_list)

if __name__ == '__main__':
    main_func()

这段代码应该在附加ResultsObject的Try-Except块中引发一个NameError(在NameError中检查字符串ResultsObject的索引是故意的(侧面思考 - 也许我应该在这里使用正则表达式...从来没有真的使用它,但它似乎是一个好的开始)))。但是,当程序到达raise命令时没有任何反应。所有工作人员继续工作,直到他们都达到相同的错误,然后程序结束而不会引发错误,也没有完成预期的工作,因为所有进程实际上都以(未提出的)错误结束。

为了测试这一点,我试图调整ProcessPoolExecutor中提供的示例代码来引发异常:

import concurrent.futures 
import math

PRIMES = [
    112272535095293,
    112582705942171,
    112272535095293,
    115280095190773,
    115797848077099,
    1099726899285419]


def is_prime(n):
    while True:
        try:
            a.append(3)
            break
        except NameError:
            if 0:
                a = [2]
            else:
                raise

    if n % 2 == 0:
        return False

    sqrt_n = int(math.floor(math.sqrt(n)))
    print(str(sqrt_n))
    for i in range(3, sqrt_n + 1, 2):
        if n % i == 0:
            return False
    return True


def main():
    with concurrent.futures.ProcessPoolExecutor(max_workers=3) as executor:
        for number, prime in zip(PRIMES, executor.map(is_prime, PRIMES)):
            print('{!s} is prime: {!r}'.format(number, prime))



if __name__ == '__main__':
    main()

这完美无缺,并返回所需的UnboundLocalVariable错误。 我想了解差异源于何处,以及如何使我的代码也按预期引发错误。 感谢您的任何见解!

0 个答案:

没有答案