Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多处理从不执行函数会在函数之前重复代码_Python_Multiprocessing_Threadpool - Fatal编程技术网

Python 多处理从不执行函数会在函数之前重复代码

Python 多处理从不执行函数会在函数之前重复代码,python,multiprocessing,threadpool,Python,Multiprocessing,Threadpool,我有一个多处理池,用一个线程运行,它在我的函数之前不断重复代码,我用不同的线程进行了尝试,而且,我做了很多类似的事情,所以我想我知道是什么导致了问题,但我不明白为什么,通常我使用argparse来解析来自用户的文件,但是我想使用输入,没有抛出错误,所以我真的没有任何线索 from colorama import Fore import colorama import os import ctypes import multiprocessing from multiprocessing impo

我有一个多处理池,用一个线程运行,它在我的函数之前不断重复代码,我用不同的线程进行了尝试,而且,我做了很多类似的事情,所以我想我知道是什么导致了问题,但我不明白为什么,通常我使用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()


如果希望che定义之前的顶级代码仅在主进程中执行,则将其放置在函数中,并在main中调用该函数


在多处理中,顶级语句将由主进程和每个子进程解释/执行。因此,如果某些代码只应由主代码执行,而不应由子代码执行,那么此类代码不应将其置于顶层。相反,这些代码应该放在函数中,并且这些函数应该在主作用域中调用,即在由uu main uu控制的if块的作用域中调用,或者在代码段的主函数中调用。

如果您希望仅在主进程中执行che定义之前的顶级代码,然后将其放在函数中,并在main中调用该函数

在多处理中,顶级语句将由主进程和每个子进程解释/执行。因此,如果某些代码只应由主代码执行,而不应由子代码执行,那么此类代码不应将其置于顶层。相反,这些代码应该放在函数中,这些函数应该在主作用域中调用,即在由uu main uu控制的if块的作用域中调用,或者在代码段的主函数中调用。

以下是您的问题:

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()
在生成Windows和MacOS或某些Unix的forkserver的操作系统上,子进程导入脚本。由于print'header'在全局范围内,它将在脚本第一次导入进程时运行,因此输出为:

header
header
header
header
func 1
func 2
func 3
多处理脚本的所有内容都应该在函数内部运行一次,并且主脚本应该通过if_name_uuu=='uu main_uuu':,调用它们一次,因此解决方案是将其移动到def main::

输出:

header
func 1
func 2
func 3
以下是您的问题:

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()
在生成Windows和MacOS或某些Unix的forkserver的操作系统上,子进程导入脚本。由于print'header'在全局范围内,它将在脚本第一次导入进程时运行,因此输出为:

header
header
header
header
func 1
func 2
func 3
多处理脚本的所有内容都应该在函数内部运行一次,并且主脚本应该通过if_name_uuu=='uu main_uuu':,调用它们一次,因此解决方案是将其移动到def main::

输出:

header
func 1
func 2
func 3

是的,修复了,抱歉,是的,在函数che被重复之前的所有代码,甚至输入不能是che,它甚至没有运行,因为我在里面放了一个测试print语句,它甚至没有打印任何东西,但我所做的只是发出get/post请求,我只是将里面的代码更改为printf,通过命令和提示符得到相同的结果,首先,我尝试使用pyinstaller使用exe进行编译,因为我认为不会有问题,但后来出现了问题,所以我打开cmd并键入python pathtofileAny idea@Carcigenicateyes修复了它抱歉,是的,在函数che被重复之前的所有代码,即使输入也不能是che,它甚至没有运行,因为我在里面放了一个test print语句,它甚至没有打印任何东西,但我所做的只是发出get/post请求。我只是将里面的代码改为printf,通过命令和提示符得到相同的结果。首先,我尝试使用pyinstaller用exe编译,因为我认为不会有问题,但后来我打开cmd,输入python-pathtofileAny-idea@Carcigenicate