python - python在Windows上进行多处理时出现运行时错误

标签 python multiprocessing deap

我正在 Windows 机器上尝试 python 的线程和多重处理。但是 python 给出以下消息。

RuntimeError: 
            Attempt to start a new process before the current process
            has finished its bootstrapping phase.
            This probably means that you are on Windows and you have
            forgotten to use the proper idiom in the main module:
                if __name__ == '__main__':
                    freeze_support()
                    ...
            The "freeze_support()" line can be omitted if the program
            is not going to be frozen to produce a Windows executable.

在 Windows 中,如果 name == 'main':因此必须完成,我的实现如下,但是在解决如何发生此类错误之后,或者这是我所做的情况不知道。 请帮助我。

import random
import numpy
import matplotlib.pyplot
import time
import multiprocessing

from deap import algorithms
from deap import base
from deap import creator
from deap import tools
# from docutils.utils.punctuation_chars import delimiters
IND_INIT_SIZE = 3000 
# MIN_ENERGY = 237178.013392/3600 
MIN_ENERGY =7255 
MIN_POWER = 303.4465137486
NBR_ITEMS = 3000 

# Create the item dictionary: item name is an integer, and value is
# a (weight, value) 2-uple.
items = {}
# Create random items and store them in the items' dictionary.
for i in range(NBR_ITEMS):
    items[i] = random.choice([[10,5],[10,10]])

creator.create("Fitness", base.Fitness, weights=(-1.0, -1.0))
creator.create("Individual", set, fitness=creator.Fitness)

toolbox = base.Toolbox()

# Attribute generator
toolbox.register("attr_item", random.randrange, NBR_ITEMS)

# Structure initializers
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_item, IND_INIT_SIZE)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)

def evalKnapsack(individual):
    energy = 0.0
    power = 0.0
    for item in individual:
        energy += items[item][1]
        power += items[item][0]
    if power < MIN_POWER or energy < MIN_ENERGY:
        return 100000000000,1000000000000
    return energy, power

def cxSet(ind1, ind2):
    """Apply a crossover operation on input sets. The first child is the
    intersection of the two sets, the second child is the difference of the
    two sets.
    """
    temp = set(ind1)                # Used in order to keep type
    ind1 &= ind2                    # Intersection (inplace)
    ind2 ^= temp                    # Symmetric Difference (inplace)
    return ind1, ind2

def mutSet(individual):
    """Mutation that pops or add an element."""
    for var in range(0,3000):
        if random.random() < 0.5:
            if len(individual) > 0:     # We cannot pop from an empty set
                individual.remove(random.choice(sorted(tuple(individual))))
            else:
                individual.add(random.randrange(NBR_ITEMS))
    return individual,

toolbox.register("evaluate", evalKnapsack)
toolbox.register("mate", cxSet)
toolbox.register("mutate", mutSet)
toolbox.register("select", tools.selSPEA2)
pool = multiprocessing.Pool(4)
toolbox.register("map", pool.map)

def main():
#     random.seed(64)
    NGEN = 5
    MU = 75
    LAMBDA = 75
    CXPB = 0.6
    MUTPB = 0.3

    pop = toolbox.population(n=MU)
    hof = tools.ParetoFront()
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", numpy.mean, axis=0)
    stats.register("std", numpy.std, axis=0)
    stats.register("min", numpy.min, axis=0)
    stats.register("max", numpy.max, axis=0)

    algorithms.eaMuPlusLambda(pop, toolbox, MU, LAMBDA, CXPB, MUTPB, NGEN, stats,
                              halloffame=hof)
    return pop, stats, hof
if __name__ == '__main__':  
    for var in range(0,5):
        start = time.time()
        pop, stats, hof= main()
        lischp=[]
        lisclp=[]
        libatthp=[]
        libattlp=[]
        ligoukei=[]
        for ind in hof:
            itemslist=[]
            print ind, ind.fitness
            for k in ind:
                itemslist.append(items[k])
            schpkazu=itemslist.count([10,5])
            lischp.append(schpkazu)
            battlpkazu=itemslist.count([10,10])
            libattlp.append(battlpkazu)
        print libatthp
        print lischp
        print libattlp
        print lisclp
        ligoukei.append(ind.fitness)
        print ligoukei
        #保存
        with open('battlpcazu.csv',mode='a')as fb:
            numpy.savetxt(fb,libattlp,fmt="%.0f",delimiter=",")
        with open('schpcazu.csv',mode='a')as fc:
            numpy.savetxt(fc,lischp,fmt="%.0f",delimiter=",")

        elapsed_time = time.time() - start
        print ("elapsed_time:{0}".format(elapsed_time)) + "[sec]"

最佳答案

Windows 上没有 os.fork() 调用,因此 python 从头开始​​运行您的脚本 对于每个新进程,除了用 包装的代码

if __name__ == '__main__':
    ...

在您的情况下,您只需要在主线程中创建进程池,因此将池初始化移到此部分(或从此部分调用的函数):

if __name__ == '__main__':  
    pool = multiprocessing.Pool(4)
    for var in range(0,5):
        ...

关于python - python在Windows上进行多处理时出现运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34853707/

相关文章:

python - Deap python 包 : create individuals with different ranges and mix of integers and floats

python - 深度 : Creating custom indivudal generator function along with fitness function

python - 如何像使用 Python 一样使用 SQL,通过复制行同时更改该行中的一个值(其中该值源自不同的列)?

multithreading - 更好地解释 TAS Vs。 "The Art Of Multiprocessor Programming"Herlihy & Shavits 上下文中的 TTAS

python - 在 __init__ 上启动新进程(对于 TCP 监听器 - 服务器)

python - 在Python多处理中引入延迟

python - 在 PyQt 中,是否可以从 QTabWidget 中分离标签?

python - 如何改变列表中的每三个元素?

python - 如何获取元素在列表中出现的次数 Python