多处理永不执行功能会在功能之前重复代码

时间:2019-12-27 23:06:59

标签: python multiprocessing threadpool

我有一个多处理池,它有1个线程运行,并且在我的函数执行之前,它一直在重复执行代码,我尝试了不同的线程,而且,我做了很多这样的事情,所以我想我知道是造成问题的原因,但我不明白为什么,通常我使用argparse来解析用户的文件,但是我想使用输入,不会抛出任何错误,所以说实话我没有任何线索。

from colorama import Fore
import colorama
import os
import ctypes
import multiprocessing
from multiprocessing import Pool
import random



colorama.init(autoreset=False) 
print("headerhere")
#as you can see i used input instead of argparse
g = open(input(Fore.RED + " File Path?: " + Fore.RESET))
gg = open(input(Fore.RED + "File Path?: " + Fore.RESET))
#I messed around with this to see if it was the problem, ultimately disabling it until i fixed it, i just use 1 thread 
threads = int(input(Fore.RED + "Amount of Threads?: " + Fore.RESET))

arrange = [lines.replace("\n", "")for lines in g]
good = [items.replace("\n", "") for items in gg]
#this is all of the code before the function that Pool calls

def che(line):
    print("f")

    #i would show my code but as i said this isnt the problem since ive made programs like this before, the only thing i changed is how i take file inputs from the user
def main():
    pool = Pool(1)
    pool.daemon = True
    result = pool.map(che, arrange)


if __name__ == "__main__":
    main()






if __name__ == "__main__":
    main()

2 个答案:

答案 0 :(得分:2)

如果只想在主进程中执行che定义之前的顶级代码,则将其放在一个函数中,然后在main中调用该函数。

在多处理中,主进程和每个子进程都将解释/执行顶级语句。因此,如果某些代码应仅由主代码而不是子代代码执行,则此类代码不应将其置于顶层。相反,应将此类代码放在函数中,并且应在主作用域(即在if控制的__main__块的范围内(或在main函数中调用)调用这些功能。在您的代码段中。

答案 1 :(得分:2)

这是您问题的minimal, reproducible example

from multiprocessing import Pool

print('header')

def func(n):
    print(f'func {n}')

def main():
    pool = Pool(3)
    pool.map(func,[1,2,3])

if __name__ == '__main__':
    main()

在以“ spawn”(Windows和MacOS)或“ forkserver”(某些Unix)为default start methods的OS上,子进程会导入您的脚本。由于print('header')在全局范围内,它将在首次将脚本导入到进程中时运行,因此输出为:

header
header
header
header
func 1
func 2
func 3

一个多处理脚本应具有在函数内部运行一次的所有功能,并且应由主脚本通过if_name__ == '__main__':调用一次,因此解决方案是将其移至def main():中:

from multiprocessing import Pool

def func(n):
    print(f'func {n}')

def main():
    print('header')
    pool = Pool(3)
    pool.map(func,[1,2,3])

if __name__ == '__main__':
    main()

输出:

header
func 1
func 2
func 3