微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如何使用 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)
    healthPixel = (241,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,49)
    attackBorder = (255,0)
    screenshot = pyautogui.screenshot()
    healthPixel = (241,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),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,duration=.2)
        pyautogui.press('F10')
        time.sleep(delay)
    else:
        didMove = False
        print('dontmove')


def doMove2():
    screenshot = pyautogui.screenshot()
    monsterPixel = (187,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 函数结束之前线程不会评估,这是我的问题..有什么解决方案吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。