Python 3.x 正在调用GPIO.add_event_detect()的call_back函数,但未停止程序中其他代码/函数的执行

Python 3.x 正在调用GPIO.add_event_detect()的call_back函数,但未停止程序中其他代码/函数的执行,python-3.x,events,bluetooth,python-multithreading,raspberry-pi4,Python 3.x,Events,Bluetooth,Python Multithreading,Raspberry Pi4,我已经使用GPIO为连接在GPIO 17和GPIO 25的按钮设置了中断。当按下按钮时,添加事件检测()和与这些GPIO关联的回调函数会被完美调用,并且在执行后退出程序,这正是我想要的 但问题是,这些回调函数没有停止在按下按钮和调用回调函数之前运行的代码的执行 这让我陷入了一个更大的问题,回调函数和main()中正在执行的函数同时运行,因为它们都涉及“engine.say()”或“aplay”命令,这导致我的蓝牙耳机流量过大,最终断开连接,无法播放任何一个引擎。回拨函数中的say(),即engi

我已经使用GPIO为连接在GPIO 17和GPIO 25的按钮设置了中断。当按下按钮时,添加事件检测()和与这些GPIO关联的回调函数会被完美调用,并且在执行后退出程序,这正是我想要的

但问题是,这些回调函数没有停止在按下按钮和调用回调函数之前运行的代码的执行

这让我陷入了一个更大的问题,回调函数和main()中正在执行的函数同时运行,因为它们都涉及“engine.say()”或“aplay”命令,这导致我的蓝牙耳机流量过大,最终断开连接,无法播放任何一个引擎。回拨函数中的say(),即engine.say(),以及main()函数中的engine.say(),我必须重新启动pi才能使蓝牙耳机音频重新工作

我想这个问题是因为回调函数与main()中的函数运行在同一个线程中,只能通过为这两个函数创建单独的线程来解决,尽管我不确定。请帮忙

这是我的密码:

def main():

# Import Libraries
import RPi.GPIO as GPIO
from time import sleep
import sys
import pyttsx3
engine = pyttsx3.init()
engine.setProperty('rate', 150)    # Speed percent
engine.setProperty('volume', 0.9)  # Volume 0-1


# Configure GPIO of Rpi
GPIO.cleanup()
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BCM) # Use GPIO pin numbering
button1 = 25 # For WhatsApp
button2 = 17 # For Careem


# Set up GPIO for Whatsapp
GPIO.setup(button1, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) # Set GPIO 25 (pin 22) to be an input pin for WhatsApp


# Set up GPIO for Careem
GPIO.setup(button2, GPIO.IN, pull_up_down = GPIO.PUD_DOWN) # Set GPIO 17 (pin 11) to be an input pin for UBER


# Call_back function for WhatsApp
def x1(button1):
    sleep(0.005) # edge debounce of 5mSec
    # only deal with valid edges
    if GPIO.input(button1) == 1:
        WhatsApp(lat_o,long_o)         # ---------> Function which contains engine.say() commands
        GPIO.remove_event_detect(button1)
        GPIO.remove_event_detect(button2)
        GPIO.cleanup([button1])
        GPIO.cleanup([button2])
        sys.exit("Stopping due to WhatsApp Interrupt!")
    return 

# Call_back function for Careem
def x2(button2):
    sleep(0.005) # edge debounce of 5mSec
    # only deal with valid edges
    if GPIO.input(button2) == 1:
        careem()           # ---------> Function which contains engine.say() commands
        GPIO.remove_event_detect(button1)
        GPIO.remove_event_detect(button2)
        GPIO.cleanup([button1])
        GPIO.cleanup([button2])
        sys.exit("Stopping due to Careem Interrupt!")
    return

# Setting up Interrupt for WhatsApp
GPIO.add_event_detect(button1 ,GPIO.RISING, callback = x1, bouncetime = 5)
# Setting up Interrupt for Careem
GPIO.add_event_detect(button2 ,GPIO.RISING, callback = x2, bouncetime = 5)


# Voice Recognition
lat_d,long_d = VR_GEO()    # ----> Function which contains engine.say() commands




while True:

    
    # GOOGLE_DIRECTIONS_API
    directions()      # --------> Function which contains engine.say() commands
   
    pass

return None
现在发生的事情是,如果我按下按钮1,即调用WhatsApp,在VR_GEO()的引擎中。say()命令正在执行时,程序执行回调函数x1(),以在播放VR_GEO()的引擎时同时执行WhatsApp()。say()我想在按下按钮后立即停止的命令,但它们会一直播放,直到VR_GEO()完成。而且,这不允许WhatsApp()的引擎要执行的所有命令以及蓝牙耳机的音频消失后的某个时间,程序背景中显示了大量与“卡号”、“蓝牙”和“blue_alsa”相关的错误,我必须重新启动并重新启动程序才能恢复音频

这也使得我的Rasberry Pi 4在这种情况下非常非常慢

因此,简言之,我有一个引擎冲突。比如()回调函数和main()中的函数

我想要的:在调用回调函数或检测到事件后立即停止执行所有其他代码


我最后一年的项目截止日期还有几周,这可能是我完成项目的最后一个障碍,所以我非常感谢任何形式的帮助。谢谢。

你似乎没有对我做任何事。此外,如果你能发布一个