Linux-使用键绑定终止AutoKey脚本

时间:2018-07-25 07:33:26

标签: python linux debian autokey

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

我确实感到奇怪的是,没有简单选项可以终止正在运行的脚本。

因此,有没有简单方法来实现这一目标。

原谅无能。 ._。

1 个答案:

答案 0 :(得分:1)

当前没有这种方法。

Autokey使用一种简单的机制来同时运行脚本:每个脚本都在单独的Python线程内执行。它使用this wrapper类使用ScriptRunner类来运行脚本。 有一些方法可以杀死正在运行的任意Python线程,但是这些方法既不是 nice 也不是 simple 。您可以在以下位置找到有关该问题的一般情况的答案:»Is there any way to kill a Thread in Python?«

有一种 nice 可能性,但这并不是真正的简单,需要脚本的支持。您可以使用全局脚本存储“发送” 停止信号到脚本。可以在here中找到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变量的值,如果它为True,则将其中断:

#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()

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