从多处理过程中获取结果 [英] Get result from multiprocessing process

查看:81
本文介绍了从多处理过程中获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法使此代码中的多处理工作.我应该更改什么,或者在多处理中是否存在其他功能可以使我执行该操作.

I want to know if is there a way to make multiprocessing working in this code. What should I change or if there exist other function in multiprocessing that will allow me to do that operation.

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

You can call the locateOnScreen('calc7key.png') function to get the screen coordinates. The return value is a 4-integer tuple: (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]

推荐答案

通常,如果您只想使用多处理来并行运行现有的CPU密集型功能,则最简单的方法是通过文档:

In general, if you just want to use multiprocessing to run existing CPU-intensive functions in parallel, it is easiest to do through a Pool, as shown in the example at the beginning of the documentation:

# ...

def getRegionOfResourceImage():

    global resourceIconRegion

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

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

这篇关于从多处理过程中获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆