如何使用Python同时执行多个任务?

2024-09-29 23:20:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让我的宏程序同时执行多个功能。我设置它的方法是定义函数,然后对大多数函数调用一个线程,使一些函数仍在主While循环上运行

好的,所以问题是:当主While循环函数运行时,我打开的不同线程在主While循环函数执行之前不会执行

我需要的是一种使之成为可能的方法,使所有函数同时执行,而不管它是否在等待主While循环函数完成

这是我的密码: 谢谢


import cv2 as cv
import numpy as np
import pyautogui
import time
from threading import Thread


healHk = 'F2'
healHk2 = 'F5'
manaHk = 'F3'
hasteHk = 'F7'
doHeal = 'yes'
doMana = 'yes'
doHaste = 'yes'
spellHk1 = 'F1'
spellHk2 = 'F5'
doMove = True
didMove = True
doFollow = False

time.sleep(5)

def healer():
    manaPixel = (83, 80, 218)
    healthPixel = (241, 97, 97)
    hastePixel = (249, 249, 248)
    im = pyautogui.screenshot()
    monsterPixel = (187, 214, 40)
    monsterLoc = (10, 49)
    matchMonster = im.getpixel(monsterLoc)
    matchMana = im.getpixel((1175, 160))
    matchHp = im.getpixel((1211, 148))
    matchHp2 = im.getpixel((1175, 148))
    matchHaste = im.getpixel((597, 63))

    global doMove
    if matchHp2 != healthPixel and doHeal == 'yes':
        time.sleep(.6)
        pyautogui.press(healHk2)
        time.sleep(.6)
        pyautogui.press(healHk2)
        time.sleep(.6)
    else:
        doMove = True
    if matchMana != manaPixel and doMana == 'yes' and matchHp2 == healthPixel:
        pyautogui.press(manaHk)
        time.sleep(.6)
        doMove = False
    else:
        doMove = True

    if matchHp != healthPixel and matchHp2 == healthPixel and doHeal == 'yes':
        time.sleep(.6)
        pyautogui.press(healHk)
        doMove = False
    else:
        doMove = True

    if matchHaste != hastePixel and doHaste == 'yes' and matchMonster != monsterPixel:
        time.sleep(.6)
        pyautogui.press(hasteHk)
        doMove = False
    else:
        doMove = True


def attacker():
    greenFollow = (92, 255, 92)
    monsterPixel = (69, 69, 69)
    monsterLoc = (14, 50)
    attackBorder = (255, 0, 0)
    healthPixel = (241, 97, 97)
    im = pyautogui.screenshot()
    matchHp = im.getpixel((1206, 148))
    matchFollow = im.getpixel((1262, 202))
    matchMonster = im.getpixel(monsterLoc)
    matchAttackRight = im.getpixel((23, 48))
    matchHp2 = im.getpixel((1175, 148))
    global doMove
    if matchFollow != greenFollow and doFollow == True:
        time.sleep(.6)
        pyautogui.leftClick(1262, 204)
        time.sleep(1)
        pyautogui.moveTo(1090, 204, duration=.3)
    if matchMonster != monsterPixel and matchAttackRight != attackBorder:
        pyautogui.press('space')
        time.sleep(.6)
        doMove = False
    else:
        doMove = True
    if matchAttackRight == attackBorder:
        doMove = False
        if matchHp2 == healthPixel:
            time.sleep(.6)
            pyautogui.press(spellHk1)

    else:
        doMove = True


sleepTime = 5.5


def charMove(delay, imgSrc):
    monsterPixel = (187, 214, 40)
    monsterLoc = (10, 49)
    attackBorder = (255, 0, 0)
    screenshot = pyautogui.screenshot()
    healthPixel = (241, 97, 97)
    matchHp = screenshot.getpixel((1211, 148))
    matchMonster = screenshot.getpixel(monsterLoc)
    matchAttackRight = screenshot.getpixel((23, 48))
    cb1 = cv.imread(imgSrc, cv.IMREAD_UNCHANGED)
    w, h = cb1.shape[: -1]
    screenshot = np.array(screenshot)
    screenshot = screenshot[:, :, ::-1].copy()
    resultLife = cv.matchTemplate(cb1, screenshot, cv.TM_CCOEFF_NORMED)
    threshold = 0.80
    locations = np.where(resultLife >= threshold)
    locations = list(zip(*locations[::-1]))
    for pt in locations:
        cv.rectangle(cb1, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2)
    global didMove
    if locations and matchHp == healthPixel and matchMonster != monsterPixel and matchAttackRight != attackBorder and doMove == True:
        didMove = True
        print('moving')
        time.sleep(.3)
        pyautogui.moveTo(pt[0] + w/2, pt[1] + h/2, duration=0.1)
        pyautogui.leftClick()
        pyautogui.moveTo(1090, 204, duration=.2)
        pyautogui.press('F10')
        time.sleep(delay)
    else:
        didMove = False
        print('dontmove')


def doMove2():
    screenshot = pyautogui.screenshot()
    monsterPixel = (187, 214, 40)
    monsterLoc = (10, 49)
    attackBorder = (255, 0, 0)
    matchMonster = screenshot.getpixel(monsterLoc)
    matchAttackRight = screenshot.getpixel((23, 48))

    if matchMonster == monsterPixel or matchAttackRight == attackBorder:
        global doMove
        doMove = False
    else:
        doMove = True


while True:

    x = Thread(target=healer)
    x.start()
    y = Thread(target=attacker)
    y.start()
    z = Thread(target=doMove2)
    z.start()
    moveTime = 5

    charMove(moveTime, 'cb1.jpg')

    charMove(moveTime, 'cb2.jpg')

    if cv.waitKey(1) == ord('q'):
        cv.destroyAllWindows()
        break
print('Done.')

注意charMove函数有一个time.sleep,在charMove函数结束之前线程不会计算,这就是我的问题。。有什么解决办法吗


Tags: and函数falsetrueiftimesleepcv

热门问题