Я хочу иметь возможность запускать 5 различных потоков, из которых реплени sh по завершении. По сути, если я запустил 5 потоков, и 1 из них завершил свою работу, они go вернулись бы к 5 потокам, пока выполнялось условие. Все эти потоки символизируют одну очередь в группе из 5 очередей. Вместо того, чтобы программа ожидала завершения каждой очереди sh перед продолжением. Я хочу запускать отдельные потоки, чтобы он мог работать асинхронно, но при этом мог выполнять максимум 5 потоков, если они не уменьшатся. Я действительно не знаю, возможно ли это, но все отзывы приветствуются, даже если это просто настройки.
def mqueue(self):
location = [(5, 2), (6, 63), (5, 116), (5, 187), (5, 241)]
while True:
time.sleep(0.25)
for index, qnumber in enumerate(self.queue):
self.qnumber = qnumber
screenshot = pyautogui.screenshot('ass.png', (40, 110, 50, 290))
pix = screenshot.load()
x, y = 40, 45
xloc, yloc = location[index]
pixel = pix[int(x / 2 + xloc), int(y / 2 + yloc)]
if pixel[0] >= 230 and pixel[1] >= 230 and pixel[2] >= 230:
self.press(qnumber)
print(qnumber)
self.checker()
Я хочу, чтобы потоки запускались с указанием c для этого метода «очереди». Метод очереди запускает метод проверки, который сам запускает поток:
def checker(self):
check = pyautogui.screenshot('check.png', (315, 600, 630, 35))
text = tess.image_to_string(check)
print(text)
for food in self.menu:
if food in text:
print(food)
t = Thread(target=self.menu[food])
t.start()
break
for chore in self.chores:
if chore in text:
print(chore)
t = Thread(target=self.chores[chore])
t.start()
break
Это весь код. Как вы понимаете, это автоматизация кулинарной игры:
from threading import Thread
import pyautogui
import pytesseract as tess
import time
class CSDPlayer:
def __init__(self):
tess.pytesseract.tesseract_cmd = r'D:/Tesseract OCR/tesseract.exe'
self.salad = {'Ranch': 'R', 'Vinaigrette': 'V', 'Thousand': 'T', 'Cheese': 'C', 'Bacon': 'B',
'Onion': 'O',
'Mushrooms': 'M', 'Greens': 'G', 'everything': ['B', 'O', 'M', 'G']}
self.steak = {'Season': 'S', 'Spice': 'P', 'Spicy': 'P', 'Juice': 'J', 'Juicy': 'J', 'Citrus': 'C'}
self.pasta = {'Cheese Sauce': 'C', 'White Sauce': 'W', 'Red Sauce': 'R', 'Meatballs': 'M', 'Chicken': 'K',
'Bacon': 'B', 'Red Peppers': 'P', 'Mushrooms': 'U', 'Spinach': 'S', 'Onions': 'O', 'Spices': 'I'}
self.soup = {'Chicken': 'K', 'Noodle': 'W', 'Cubes': 'U', 'Seasoning': 'S', 'Potatoes': 'P',
'Red Peppers': 'D'}
self.soupactive = {'Tomato': 'T', 'Carrot': 'A', 'Celery': 'Y', 'Lettuce': 'L'}
self.icecream = [{'Vanilla': 'V', 'Chocolate': 'C', 'Mint Chocolate': 'M', 'Butter Pecan': 'B'},
{'Cherry': 'H', 'Sprinkles': 'P', 'Whip': 'W', 'Nuts': 'N', 'Nots': 'N', 'Chocolate Sauce': 'O',
'Strawberry Sauce': 'S', 'all': ['H', 'P', 'W', 'N', 'O', 'S']}]
self.number = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
self.ordnumber = {'once': 1, 'twice': 2, 'thrice': 3}
self.queue = ['1', '2', '3', '4', '5']
self.menu = {'Fried Chicken': self.mfriedchicken, 'Steak': self.msteak, 'Soup': self.msoup}
self.chores = {'Clean': self.mtoilet, 'Dishes': self.mdishes, 'Trash': self.mtrash, 'Rodents': self.mrodents}
self.menuupdate()
def menuupdate(self):
self.menu.update(self.menu.fromkeys(['A Thousand Flavors', 'Tomato Ranch', 'Thousand Tomatoes', 'Salad Verde',
'The Oily Greens', 'The Thousand Salad', 'Cheesy Peppers', 'Kids Delight',
'The Dry Deluxe', 'The Mix', 'House Salad', 'The Dry Greens', 'The Oil Bleu',
'Cheesy Leaves', 'Vinaigrette Classic', 'Three Thousand', 'Thousand House',
'Pepper Ranch', 'The Big Salad', 'Vinaigrette House', 'The Thousand Peppers',
'The Manhattan', 'The Oily Pepper', 'Cheesy Vinaigrette'],
self.msalad))
self.menu.update(self.menu.fromkeys(['Spaghetti Deluxe', 'Spicy Bacon Pasta', 'Creamy Meat Pasta', 'Cheese Pasta',
'Classic Spaghetti', 'Cheesy Veggie Pasta', 'The Carbonara', 'Red Veggie Pasta',
'Cheesy Meaty Pasta', 'Creamy Alfredo', 'Cheesy Chicken Pasta', 'Red Deluxe',
'Hot Bacon Pasta', 'Spicy Spaghetti', 'Creamy Veggie Pasta', 'The Meaty Pasta',
'Cheesy Onion Pasta', 'Cheesy Onion Pasta', 'Cheesy Deluxe Pasta'],
self.mpasta))
self.menu.update(self.menu.fromkeys(['Cherry Vanilla', 'Chocolate Sprinkles', 'The Fiesta Bowl', 'Minty Deluxe',
'Nutty Vanilla', 'Nutty Chocolate', 'Vanilla Dream', 'Buttery Nuts',
'Deluxe Butter Pecan', 'Nutty Mint', 'Trio of Delicious', 'Plain Vanilla',
'Chocolate Heaven', 'Vanilla and Chocolate', 'The Yin and Yan', 'Mint Cherry',
'Plain Chocolate', 'Birthday Surprise'],
self.micecream))
self.menu.update(self.menu.fromkeys(['Casu Marzu', 'Le Cheap Valu-Wine'], self.mwine))
def capture(self):
self.image = pyautogui.screenshot('instructions.png', (315, 640, 750, 80))
self.text = tess.image_to_string(self.image)
self.instructions = self.text.split(' ')
def checker(self):
check = pyautogui.screenshot('check.png', (315, 600, 630, 35))
text = tess.image_to_string(check)
print(text)
for food in self.menu:
if food in text:
print(food)
t = Thread(target=self.chores[chore])
t.start()
break
for chore in self.chores:
if chore in text:
print(chore)
t = Thread(target=self.chores[chore])
t.start()
break
def mqueue(self):
location = [(5, 2), (6, 63), (5, 116), (5, 187), (5, 241)]
while True:
time.sleep(0.25)
for index, qnumber in enumerate(self.queue):
self.qnumber = qnumber
screenshot = pyautogui.screenshot('ass.png', (40, 110, 50, 290))
pix = screenshot.load()
x, y = 40, 45
xloc, yloc = location[index]
pixel = pix[int(x / 2 + xloc), int(y / 2 + yloc)]
if pixel[0] >= 230 and pixel[1] >= 230 and pixel[2] >= 230:
self.press(qnumber)
print(qnumber)
self.checker()
@staticmethod
def press(key, pause=0.05, presses=1):
for _ in range(presses):
pyautogui.keyDown(key)
time.sleep(pause)
pyautogui.keyUp(key)
def mtoilet(self):
self.press('down')
self.press('s')
def mdishes(self):
for _ in range(3):
for _ in range(2):
self.press('left')
self.press('right')
self.press('up')
def mtrash(self):
for _ in range(2):
self.press('up', 0.25)
self.press('right')
self.press('s')
def mrodents(self):
self.press('right')
self.press('down')
self.press('c')
self.press('s')
def mbeer(self):
self.press('down', 1.25)
self.press('enter')
def mwine(self):
self.capture()
if 'Casu Marzu' in self.text:
self.press('w')
self.press('up', presses=25)
self.press('enter')
def msalad(self):
self.capture()
for topping in self.salad:
if topping in self.text:
if topping != 'everything':
self.press(self.salad[topping])
print(self.salad[topping])
else:
for letter in self.salad[topping]:
self.press(letter)
self.press('enter')
def mfriedchicken(self):
self.press('down', 3.5)
self.press('p')
self.press('enter')
def mfish(self):
self.capture()
self.press('left')
self.press('down')
self.press('right')
self.press('s')
if 'Lemon' in self.text:
self.press('l')
self.press('enter')
time.sleep(8)
self.press('1')
def mchickenbreast(self):
self.press('t', presses=6)
self.press('s')
self.press('enter')
time.sleep(15)
self.press('1')
def msteak(self):
self.capture()
flavors = []
numbers = []
for instruction in self.instructions:
for flavor in self.steak:
if flavor in instruction:
flavors.append(self.steak[flavor])
for number in self.number:
if number in instruction.lower():
numbers.append(self.number[number])
for ord_number in self.ordnumber:
if ord_number in instruction.lower():
numbers.append(self.ordnumber[ord_number])
if 'S' in flavors:
del numbers[flavors.index('S')]
if len(flavors) != len(numbers):
numbers.insert(0, 1)
for letter, times in zip(flavors, numbers):
self.press(letter, presses=times)
self.press('enter')
time.sleep(17.5)
self.press('1')
def mpasta(self):
self.capture()
self.press('R')
self.press('enter')
time.sleep(11)
self.press('1')
self.capture()
for topping in self.pasta:
if topping in self.text:
self.press(self.pasta[topping])
print(self.pasta[topping])
self.press('enter')
def msoup(self):
self.capture()
ingredient = []
cutingredient = []
print(self.text)
for ing in self.soup:
if ing in self.text:
ingredient.append(ing)
for cuting in self.soupactive:
if cuting in self.text:
cutingredient.append(cuting)
for normal in ingredient:
self.press(self.soup[normal])
for active in cutingredient:
self.press(self.soupactive[active])
self.press('Down', presses=3)
self.press('enter')
time.sleep(11)
self.press('1')
# self.press(self.qnumber)
def micecream(self):
self.capture()
flavors = []
numbers = []
extras = []
choc_count = 0
for number in self.number:
if number in self.text.lower():
for _ in range(self.text.lower().count(number)):
numbers.append(self.number[number])
for flavor in self.icecream[0]:
if flavor in self.text:
if flavor != 'Chocolate':
flavors.append(self.icecream[0][flavor])
else:
if 'Mint' in self.text:
if self.text[self.text.find('Mint'):self.text.find('Chocolate'):1] != 'Mint ':
flavors.append(self.icecream[0][flavor])
else:
flavors.append(self.icecream[0][flavor])
for extra in self.icecream[1]:
if extra in self.text:
if extra != 'all':
extras.append(self.icecream[1][extra])
if 'all' in self.text:
for char in self.icecream[1]['all']:
extras.append(char)
print(extras)
if 'except' in self.text:
print(extras)
rmv1 = extras[0]
rmv2 = extras[1]
for _ in range(2):
extras.remove(rmv1)
extras.remove(rmv2)
print(extras)
if len(flavors) != len(numbers):
numbers.append(1)
print(self.text)
print(numbers)
print(flavors)
print(extras)
for letter, times in zip(flavors, numbers):
self.press(letter, presses=times)
for extra in extras:
self.press(extra)
self.press('enter')
player = CSDPlayer()
player.mqueue()