You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python3同时运行两个无限循环并切换执行控制权的实现方案咨询

问题解决方案

1. 实现两个函数并发运行的方案

你当前的代码属于串行执行逻辑,两个函数都是无限循环结构,调用第一个函数后程序会一直卡在该循环内,永远无法执行第二个函数。
由于你的场景以屏幕读取、sleep等待这类IO操作为主,优先推荐用多线程实现,资源开销小,共享变量也更方便。如果你的图像处理逻辑是CPU密集型(比如大量矩阵运算),再考虑用多进程规避GIL限制。
因为屏幕属于独占资源,需要加锁避免两个线程同时访问产生冲突,参考实现代码如下:

import my_img_proc as mip
import my_app_proc as map
import time
import threading

# 屏幕访问互斥锁,保证同一时间只有一个线程操作屏幕
screen_lock = threading.Lock()

class myprocess:
    def perform_image_processing(self,image_name):
        while True:
            with screen_lock:
                mip.perform_some_image_processing_from_screen(image_name)
            time.sleep(180)

    def perform_another_processing(self,inp):
        while True:
            with screen_lock:
                map.perform_some_other_image_processing_from_screen(inp)
            time.sleep(180)


mp = myprocess()
# 创建两个独立线程分别绑定两个执行函数
t1 = threading.Thread(target=mp.perform_image_processing, args=(my_image,))
t2 = threading.Thread(target=mp.perform_another_processing, args=(2,))
# 启动线程
t1.start()
t2.start()
# 阻塞主线程等待子线程执行(因为是无限循环,会一直保持运行)
t1.join()
t2.join()

2. 共享变量传递控制权的方案可行性及实现

这个方案完全可行,比无差别并发的方案灵活度更高,你可以自主控制任务的执行顺序,后续扩展更多同类任务也更方便。
实现时要注意线程安全,不要直接读写普通全局/共享变量,要么加锁保证读写原子性,要么直接用Python threading模块自带的Event工具,比普通1/0变量更易用。
参考实现代码如下,完全满足你“执行完当前迭代再切换控制权”的需求:

import my_img_proc as mip
import my_app_proc as map
import time
import threading

class myprocess:
    def __init__(self):
        # 控制权标志:1=perform_image_processing可执行,0=perform_another_processing可执行
        self.control_flag = 1
        # 标志锁,保证控制权读写的原子性,避免多线程竞争出错
        self.flag_lock = threading.Lock()

    def perform_image_processing(self,image_name):
        while True:
            # 检查是否拿到控制权
            with self.flag_lock:
                if self.control_flag != 1:
                    time.sleep(1) # 未拿到控制权则等待1秒后重试
                    continue
            # 执行当前任务逻辑,不会被中途打断
            mip.perform_some_image_processing_from_screen(image_name)
            # 执行完成后交出控制权
            with self.flag_lock:
                self.control_flag = 0
            time.sleep(180)

    def perform_another_processing(self,inp):
        while True:
            with self.flag_lock:
                if self.control_flag != 0:
                    time.sleep(1)
                    continue
            map.perform_some_other_image_processing_from_screen(inp)
            with self.flag_lock:
                self.control_flag = 1
            time.sleep(180)


mp = myprocess()
t1 = threading.Thread(target=mp.perform_image_processing, args=(my_image,))
t2 = threading.Thread(target=mp.perform_another_processing, args=(2,))
t1.start()
t2.start()

如果需要支持“任务执行超过3分钟就中途暂停切换”的逻辑,只需要把处理逻辑拆分为可中断的小步骤,每执行一个小步骤前检查一次控制权标志即可。你现有可运行的护盾采集逻辑,本质就是单线程下的控制权切换实现,后续要扩展更多任务时,只需要把控制权标志改成任务队列的形式,就能支持N个任务轮流执行。


内容的提问来源于stack exchange,提问作者Bishnu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 01:06:06