如何避免创建带有OS.System的多个Python流程?

发布于 2025-02-08 15:13:00 字数 1547 浏览 3 评论 0原文

当连续2个IF语句执行时,我的Python程序会关闭。但这不是我想要的。我希望它再次循环而不是退出脚本。我简单地循环发现的问题是,每当os.System(命令)行运行时,都会创建Python进程。我已经单独尝试了以下操作:

os.exit()

sys.exit()

def kill_process():
    pid = os.getpid()
    sig = signal.SIGKILL
    os.kill(pid, sig)

所有这些选项都与os.System(“ Python33)单独配对script.py”),但这些都没有解决问题。每种情况都可以简单地退出脚本。

我该如何做到这一点,以便传递os.System(命令)时,它只是在不杀死/退出脚本而不每次创建另一个Python进程的情况下再次循环?

这是所讨论的功能:

def bluetoothLoop():
    while True:
        time.sleep(5)
        BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
        BT_state = BT_state.stdout
        sound = "Blow"
        title = "TURN OFF BLUETOOTH"
        message = "-------------------------"
        if "State: On" in BT_state and not "  Connected:" in BT_state:
            time.sleep(1)
            BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
            BT_state = BT_state.stdout
            if "State: On" in BT_state and not "  Connected:" in BT_state:
                command = f'''
                osascript -e 'display notification "{message}" with title "{title}" sound name "{sound}"'
                '''
                os.system(command)
                os.exit()
                time.sleep(1)
                notify.restart()

谢谢一堆,我已经为此挣扎了一段时间。

When the 2 consecutive if statements executes, my python program shuts down. But that's not what I want. I want it to loop again and not exit the script. The problem that I found with simply looping is that python processes are created every time the os.system(command) line runs. I've individually tried the following:

os.exit()

sys.exit()

def kill_process():
    pid = os.getpid()
    sig = signal.SIGKILL
    os.kill(pid, sig)

All of those options were individually paired with a os.system("python3 script.py"), yet none of these did the trick. Every scenario simply exits the script.

How do I make it so that when os.system(command) is passed that it just loops again without killing/exiting the script and without creating another python process everytime?

Here's the function in question:

def bluetoothLoop():
    while True:
        time.sleep(5)
        BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
        BT_state = BT_state.stdout
        sound = "Blow"
        title = "TURN OFF BLUETOOTH"
        message = "-------------------------"
        if "State: On" in BT_state and not "  Connected:" in BT_state:
            time.sleep(1)
            BT_state = subprocess.run(['''system_profiler SPBluetoothDataType'''], shell=True, capture_output=True, encoding="utf", errors="ignore")
            BT_state = BT_state.stdout
            if "State: On" in BT_state and not "  Connected:" in BT_state:
                command = f'''
                osascript -e 'display notification "{message}" with title "{title}" sound name "{sound}"'
                '''
                os.system(command)
                os.exit()
                time.sleep(1)
                notify.restart()

Thanks a bunch, I've been struggling with this for a while now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文