Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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_Python Multiprocessing - Fatal编程技术网

Python多处理-使用数组()、锁()和池()

Python多处理-使用数组()、锁()和池(),python,multiprocessing,python-multiprocessing,Python,Multiprocessing,Python Multiprocessing,我为这个项目奋斗了一段时间 我写这个程序是为了学习如何在不同的CPU上同时运行相同的程序,并减少处理时间 程序本身并不复杂,只是检测不同目录中是否有一些屏幕截图,但正如我之前所说,我的最终目标不是检测这些文件,而是学习多处理 为了便于测试,我没有读取文件或用户输入,而是设置了这些变量,这样您就不必输入这些内容。我用这种方法测试会更快 代码如下: from multiprocessing import Array, Lock, Pool def DetectSCREENSHOT(file, co

我为这个项目奋斗了一段时间

我写这个程序是为了学习如何在不同的CPU上同时运行相同的程序,并减少处理时间

程序本身并不复杂,只是检测不同目录中是否有一些屏幕截图,但正如我之前所说,我的最终目标不是检测这些文件,而是学习多处理

为了便于测试,我没有读取文件或用户输入,而是设置了这些变量,这样您就不必输入这些内容。我用这种方法测试会更快

代码如下:

from multiprocessing import Array, Lock, Pool

def DetectSCREENSHOT(file, counter, total): 

    # To check if this function is ever accessed
    print 'Entered in DetectSCREENSHOT function'

    if file.startswith('Screenshot') == False:

        print ' (%s %%) ERROR: the image is not a screenshot ' %(round((float(counter)/total)*100, 1))
        return 0

    else:       

        print ' (%s %%) SUCCESS: the image is a screenshot' %(round((float(counter)/total)*100, 1))
        return 1

def DetectPNG(file_list, counter_success_errors, lock):

    # To check if this function is ever accessed
    print 'Entered in DetectPNG function'

    for i in range(len(file_list)):

        file = file_list[i]

        # If the file is an .png image:

        if file.endswith('.png'):

            lock.acquire()
            counter_success_errors[0] = counter_success_errors[0] + 1
            lock.release()

            if DetectSCREENSHOT(file, counter_success_errors[0], len(file_list)) == 1:

                lock.acquire()
                counter_success_errors[1] = counter_success_errors[1] + 1
                lock.release()

            else:

                lock.acquire()
                counter_success_errors[2] = counter_success_errors[2] + 1
                lock.release()

def Main():

    # file_list = # List of lists of files in different directories
    file_list = [['A.png', 'B.png', 'C.txt', 'Screenshot_1.png'], ['D.txt', 'Screenshot_2.png', 'F.png', 'Screenshot_3.png']]

    # Array where the first cell is a counter, the second one, the number of successes and the third one, the number of errors.
    counter_success_errors = Array('i', 3)
    lock = Lock()

    counter_success_errors[0] = 0
    counter_success_errors[1] = 0
    counter_success_errors[2] = 0

    # Number of CPUS's to be used (will set the number of different processes to be run)
    # CPUs = raw_input('Number of CPUs >> ')
    CPUs = 2
    pool = Pool(processes=CPUs)

    for i in range(CPUs):
        pool.apply_async(DetectPNG, [file_list[i], counter_success_errors, lock])

    pool.close()
    pool.join()

    success = int(counter_success_errors[1])
    errors = int(counter_success_errors[2])

    print(' %s %s %s successfully. %s %s.' %('Detected', success, 'screenshot' if success == 1 else 'screenshots', errors, 'error' if errors == 1 else 'errors'))

#################################################

if __name__ == "__main__":
Main()
当我执行它时,我没有得到任何错误,但它看起来甚至无法访问DetectPNG和DetectSCREENSHOT函数

代码中有什么错误


输出:
成功检测到0个屏幕截图。0个错误。

您有缩进错误。将`打印输出('%s%s%s成功。%s%s.`添加到您的问题中。为什么要将
池放在
for
循环中。在
for
循环中应用\u async
?@CoMartel,因为要使用的CPU数量将是用户输入,因此这是我找到的实现此目的的唯一方法。
对象已具有CPU数量(并将相应地启动进程),您的
for
循环只会多次启动同一个池,这可能会导致您的问题。@CoMartel是的,但不使用所有CPU如何,我的意思是,我有8个CPU,但当我运行程序时,我可能希望使用2,或者下次使用7,这是用户输入的数字