好的,我在我的elementaryOS设备上使用AutoKey应用程序是新手,在尝试一些自定义脚本。
我发现奇怪的是,没有一个简单的选项来终止正在运行的脚本。
所以,有没有任何好用且简单的方法来实现这个功能。
请谅解我的无能。._.
目前还没有这样的方法。
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变量的值,并在其为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()
对于每个热点或长时间运行的代码路径,您应该添加此类停止检查。如果脚本中出现死锁,则无法解决此问题。
store.get_global_value()
而只能使用store.GLOBALS.get()
?后者似乎是唯一可行的。 - Cyriac Antony