Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

python - Get result from multiprocessing process

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.

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).

I got error:

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]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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):
        # ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...