Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...