从多处理过程中获取结果

时间:2018-03-29 14:03:04

标签: python multiprocessing

我想知道是否有办法让多处理工作在这段代码中。我应该更改什么,或者在多处理中是否存在允许我执行该操作的其他功能。

您可以调用locateOnScreen('calc7key.png')函数来获取屏幕坐标。返回值是一个4整数元组:(left, top, width, height)

我收到了错误:

      
checkNumber1 = resourceBlankLightTemp[1]

TypeError: 'Process' object does not support indexing
import pyautogui, time, os, logging, sys, random, copy
import multiprocessing as mp

BLANK_DARK = os.path.join('images', 'blankDark.png')
BLANK_LIGHT = os.path.join('images', 'blankLight.png')

def blankFirstDarkResourcesIconPosition():
    blankDarkIcon = pyautogui.locateOnScreen(BLANK_DARK)
    return blankDarkIcon


def blankFirstLightResourcesIconPosition():
    blankLightIcon = pyautogui.locateOnScreen(BLANK_LIGHT)
    return blankLightIcon


def getRegionOfResourceImage():

    global resourceIconRegion

    resourceBlankLightTemp = mp.Process(target = blankFirstLightResourcesIconPosition)
    resourceBlankDarkTemp = mp.Process(target = blankFirstDarkResourcesIconPosition)

    resourceBlankLightTemp.start()
    resourceBlankDarkTemp.start()

    if(resourceBlankLightTemp == None):
        checkNumber1 = 2000
    else:
        checkNumber1 = resourceBlankLightTemp[1]

    if(resourceBlankDarkTemp == None):
        checkNumber2 = 2000
    else:
        checkNumber2 = resourceBlankDarkTemp[1]

1 个答案:

答案 0 :(得分:0)

通常,如果您只想使用多处理来并行运行现有的CPU密集型函数,最简单的方法是Pool,如the documentation开头的示例所示。 :

# ...

def getRegionOfResourceImage():

    global resourceIconRegion

    with mp.Pool(2) as p:
        resourceBlankLightTemp, resourceBlankDarkTemp = p.map(
            lambda x: x(), [blankFirstLightResourcesIconPosition,
                            blankFirstDarkResourcesIconPosition])

    if(resourceBlankLightTemp == None):
        # ...