多处理暂停 1 个代码而另一个处于活动状态?
Multiprocessing pause 1 code while the other is active?
我正在为点击游戏制作一个机器人,该游戏有随机弹出的广告。使用下面的代码,只要检测到这些弹出窗口,它们就会被关闭。但是,播放唱首歌游戏的代码是定时的,每当发现广告时,它就会扰乱代码,使程序失败。
有谁知道每当 ad() 找到什么东西时可以暂停 play() 中发生的事情吗?
我的代码:
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
from multiprocessing import Process
import sys
def ad():
adloop = True
while adloop:
cross = pyautogui.locateOnScreen('image.png', confidence = 0.95)
if cross != None:
print("Popup!")
else:
print("Checking for popups...")
def count():
amount = 0
count = True
while count:
amount += 1
print(amount)
time.sleep(1)
if __name__=='__main__':
p1 = Process(target = ad)
p1.start()
p2 = Process(target = count)
p2.start()
您可以使用 Process.Event
作为标志。
注意操作需要pip install pyautogui pillow opencv-python
import pyautogui
import time
from multiprocessing import Process,Event
def ad(e):
adloop = True
while adloop:
cross = pyautogui.locateOnScreen('image.png', confidence = 0.95)
if cross != None:
print("Popup!")
e.clear()
else:
print("Checking for popups...")
e.set()
def count(e):
amount = 0
while True:
e.wait() # sleep process when event is clear.
amount += 1
print(amount)
time.sleep(1)
if __name__=='__main__':
# Create an event to share between the processes.
# When set, the counting process will count.
e = Event()
e.set()
# Make processes daemons, so exiting main process will kill them.
p1 = Process(target = ad, args=(e,), daemon=True)
p1.start()
p2 = Process(target = count, args=(e,), daemon=True)
p2.start()
input('hit ENTER to exit...')
输出:
hit ENTER to exit...1
Checking for popups...
2
Checking for popups...
Checking for popups...
3
Checking for popups...
4
Checking for popups...
Popup!
Popup!
Popup!
Popup!
Popup!
Popup!
Popup!
Popup!
Checking for popups...
5
Checking for popups...
6
Checking for popups...
Checking for popups...
7
我正在为点击游戏制作一个机器人,该游戏有随机弹出的广告。使用下面的代码,只要检测到这些弹出窗口,它们就会被关闭。但是,播放唱首歌游戏的代码是定时的,每当发现广告时,它就会扰乱代码,使程序失败。 有谁知道每当 ad() 找到什么东西时可以暂停 play() 中发生的事情吗?
我的代码:
from pyautogui import *
import pyautogui
import time
import keyboard
import random
import win32api, win32con
from multiprocessing import Process
import sys
def ad():
adloop = True
while adloop:
cross = pyautogui.locateOnScreen('image.png', confidence = 0.95)
if cross != None:
print("Popup!")
else:
print("Checking for popups...")
def count():
amount = 0
count = True
while count:
amount += 1
print(amount)
time.sleep(1)
if __name__=='__main__':
p1 = Process(target = ad)
p1.start()
p2 = Process(target = count)
p2.start()
您可以使用 Process.Event
作为标志。
注意操作需要pip install pyautogui pillow opencv-python
import pyautogui
import time
from multiprocessing import Process,Event
def ad(e):
adloop = True
while adloop:
cross = pyautogui.locateOnScreen('image.png', confidence = 0.95)
if cross != None:
print("Popup!")
e.clear()
else:
print("Checking for popups...")
e.set()
def count(e):
amount = 0
while True:
e.wait() # sleep process when event is clear.
amount += 1
print(amount)
time.sleep(1)
if __name__=='__main__':
# Create an event to share between the processes.
# When set, the counting process will count.
e = Event()
e.set()
# Make processes daemons, so exiting main process will kill them.
p1 = Process(target = ad, args=(e,), daemon=True)
p1.start()
p2 = Process(target = count, args=(e,), daemon=True)
p2.start()
input('hit ENTER to exit...')
输出:
hit ENTER to exit...1
Checking for popups...
2
Checking for popups...
Checking for popups...
3
Checking for popups...
4
Checking for popups...
Popup!
Popup!
Popup!
Popup!
Popup!
Popup!
Popup!
Popup!
Checking for popups...
5
Checking for popups...
6
Checking for popups...
Checking for popups...
7