Linux - 使用键绑定终止 AutoKey 脚本
Linux - Terminating AutoKey script using keybindings
好的,我是 elementaryOS 设备上的 AutoKey 应用程序的新手,我只是在玩一些自定义脚本。
我发现奇怪的是没有 简单 选项来终止 运行 脚本。
那么,有没有什么好的和简单的方法来实现这个。
恕我无能。 ._.
暂无此方法
Autokey 使用一种简单的机制来同时 运行 脚本:每个脚本都在单独的 Python 线程中执行。它使用 this wrapper to run scripts using the ScriptRunner class。
有一些方法可以杀死任意 运行ning Python 线程,但这些方法既不 nice 也不 simple。您可以在此处找到此问题的一般情况的答案:»Is there any way to kill a Thread in Python?«
有一种很好的可能性,但它并不是真的简单并且需要您的脚本的支持。您可以使用全局脚本存储向脚本 »发送« stop 信号。 API 文档可以在 here:
找到
假设,这是您要中断的脚本:
#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()
您应该为每个热或长 运行ning 代码路径添加这样的停止检查。
如果您的脚本出现死锁,这将无济于事。
好的,我是 elementaryOS 设备上的 AutoKey 应用程序的新手,我只是在玩一些自定义脚本。
我发现奇怪的是没有 简单 选项来终止 运行 脚本。
那么,有没有什么好的和简单的方法来实现这个。
恕我无能。 ._.
暂无此方法
Autokey 使用一种简单的机制来同时 运行 脚本:每个脚本都在单独的 Python 线程中执行。它使用 this wrapper to run scripts using the ScriptRunner class。 有一些方法可以杀死任意 运行ning Python 线程,但这些方法既不 nice 也不 simple。您可以在此处找到此问题的一般情况的答案:»Is there any way to kill a Thread in Python?«
有一种很好的可能性,但它并不是真的简单并且需要您的脚本的支持。您可以使用全局脚本存储向脚本 »发送« stop 信号。 API 文档可以在 here:
找到假设,这是您要中断的脚本:
#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()
您应该为每个热或长 运行ning 代码路径添加这样的停止检查。 如果您的脚本出现死锁,这将无济于事。