首页 > 解决方案 > 同时运行 2 个无限循环切换控制 - Python3

问题描述

我正在构建一个读取当前屏幕并进行一些处理的图像处理应用程序。代码骨架如下:

import my_img_proc as mip
import my_app_proc as map
import time

class myprocess:
    def perform_image_processing(self,image_name):
        while True:
            mip.perform_some_image_processing_from_screen(image_name)
            time.sleep(180) # Switch to perform_another_processing as next iteration is not needed for next 3 minutes. If this processing takes more than 3 minutes, complete current iteration and then switch to the other function.

    def perform_another_processing(self,inp):
        while True:
            map.perform_some_other_image_processing_from_screen(inp)
            time.sleep(180) # Switch to perform_image_processing as next iteration is not needed for next 3 minutes. If this processing takes more than 3 minutes, pause it and switch to the other function.


mp = myprocess()
mp.perform_image_processing(my_image) # Calling of image processing to identify items from the screen capture
mp.perform_another_processing(2) # Calling of application processing to calculate values based on screen capture

截至目前,我一次只能运行一个功能。

这里的问题是:

  1. 假设这两个功能可能需要同时访问/切换同一个屏幕,我如何同时运行它们(作为 2 个单独的线程/进程??)。

  2. 我想到的一个选项是两个函数都设置一个公共变量(到 1/0)并在继续睡眠之前将执行控制权传递给对方?可能吗?我该如何实施?

在这方面的任何帮助都将帮助我在我的应用程序中添加多个其他类似的功能。

谢谢

比什努

笔记:

对于所有无法想象我想要实现的目标的人,这是我的代码可以正常工作。此机器人代码将检查屏幕以屏蔽(以防止攻击)或收集(王国中的资源)

        def shield_and_gather_loop(self):
        current_shield_sts = self.renew_shield()
        num_of_gatherers = self.num_of_troops_gathering()
        gather_sleep = False if num_of_gatherers < self.Max_Army_Size else True
        shield_sleep = True if current_shield_sts == 'SHIELDED' else False
        shield_timeout= time.time() + 60 * 3 if shield_sleep == True else None
        while True:
            while shield_sleep == False: #and current_shield_sts != 'SHIELDED':
                self.reach_home_screen_from_anywhere()
                current_shield_sts = self.renew_shield()
                print("Current Shield Status @ ", datetime.datetime.now(), " :", current_shield_sts)
                shield_sleep = True
            #self.go_to_sleep("Shield Check ", hours=0, minutes=3, seconds=0)
                shield_timeout = time.time() + 60 * 3  #3 minutes from now
            while num_of_gatherers < self.Max_Army_Size and gather_sleep == False:
                if time.time() < shield_timeout:
                    self.device.remove_processed_files()
                    self.reach_kd_screen_from_anywhere()
                    self.found_rss_tile = 0
                    self.find_rss_tile_for_gather(random.choice(self.gather_items))
                    num_of_gatherers = self.num_of_troops_gathering()
                    print("Currently gathering: ", str(num_of_gatherers))
            if gather_sleep == True and shield_sleep == True:
                print("Both gather and shield are working fine.Going to sleep for 2 minutes...")
                time.sleep(120)
                # Wake up from sleep and recheck shield and gather status
                current_shield_sts = self.renew_shield()
                num_of_gatherers = self.num_of_troops_gathering()
                gather_sleep = False if num_of_gatherers < self.Max_Army_Size else True
                shield_sleep = True if current_shield_sts == 'SHIELDED' else False

标签: python-3.x

解决方案


似乎显而易见的事情是这样做,因为它满足您一次只运行两个动作之一的标准:

import my_img_proc as mip
import my_app_proc as map
import time

class myprocess:
    def perform_image_processing(self,image_name,inp):
        while True:
            mip.perform_some_image_processing_from_screen(image_name)
            map.perform_some_other_image_processing_from_screen(inp)
            time.sleep(180)

mp = myprocess()
mp.perform_image_processing(my_image,2)

如果您需要更复杂的东西(例如perform_some_other_image_processing_from_screen()能够在调用perform_some_image_processing_from_screen()返回之前中断调用),这可能需要线程,但它还需要您的perform_*()函数序列化对它们正在操作的数据的访问,这会使它们变慢并且更复杂,并且根据示例底部提供的注释,在第一个函数完成其识别任务之前,第二个函数听起来不能做任何有用的工作。

作为一般原则,将while True: [...] sleep(180)循环移出函数调用并进入顶级代码可能是值得的,以便调用者可以直接控制其行为。调用一个从不返回的函数不会给调用者留下太多的定制空间。

import my_img_proc as mip
import my_app_proc as map
import time

class myprocess:
    def perform_image_processing(self,image_name,inp):
       mip.perform_some_image_processing_from_screen(image_name)  
       map.perform_some_other_image_processing_from_screen(inp)

mp = myprocess()
while True:
   mp.perform_image_processing(my_image,2)
   sleep(180)

推荐阅读