3

好的,所以我是在我的elementaryOS设备上使用AutoKey 应用程序的新手,我只是在玩一些自定义脚本。

我确实觉得奇怪的是没有简单的选项来终止正在运行的脚本。

那么,有没有什么好的简单的方法来实现这一点。

原谅无能。._.

4

1 回答 1

3

目前,没有这样的方法。

Autokey 使用一种简单的机制来同时运行脚本:每个脚本都在一个单独的 Python 线程中执行。它使用这个包装器来运行使用ScriptRunner类的脚本。有一些方法可以杀死任意运行的 Python 线程,但这些方法既不也不简单。您可以在此处找到此问题的一般情况的答案:»有没有办法在 Python 中杀死线程?«

有一种很好的可能性,但它并不简单,需要您的脚本支持。您可以使用全局脚本存储向脚本“发送”停止信号。API 文档可以在这里找到:

假设,这是您要中断的脚本:

#Your script
import time
def crunch():
    time.sleep(0.01)
def processor():
    for number in range(100_000_000):
        crunch(number)
processor()

将这样的停止脚本绑定到热键:

store.set_global_value("STOP", True)

并修改您的脚本以轮询 STOP 变量的值,如果为真则中断:

#Your script
import time
def crunch():
    time.sleep(0.01)
def processor():
    for number in range(100_000_000):
        crunch(number)
        # Use the GLOBALS directly. If not set, use False as the default.
        if store.GLOBALS.get("STOP", False):
            # Reset the global variable, otherwise the next script will be aborted immediately.
            store.set_global_value("STOP", False)
            break
processor()

您应该为每个热运行或长时间运行的代码路径添加这样的停止检查。如果脚本中出现死锁,这将无济于事。

于 2018-07-27T12:46:02.403 回答