【发布时间】:2019-01-01 22:34:40
【问题描述】:
好的,所以我是 elementaryOS 设备上的 AutoKey 应用程序的新手,我只是在玩一些自定义脚本。
我确实觉得奇怪的是,没有 simple 选项来终止正在运行的脚本。
那么,有没有什么不错的和简单的方法来实现这一点。
请原谅我的无能。 ._.
【问题讨论】:
标签: python linux debian autokey
好的,所以我是 elementaryOS 设备上的 AutoKey 应用程序的新手,我只是在玩一些自定义脚本。
我确实觉得奇怪的是,没有 simple 选项来终止正在运行的脚本。
那么,有没有什么不错的和简单的方法来实现这一点。
请原谅我的无能。 ._.
【问题讨论】:
标签: python linux debian autokey
目前还没有这种方法。
Autokey 使用一种简单的机制来同时运行脚本:每个脚本都在一个单独的 Python 线程中执行。它使用this wrapper 来运行使用ScriptRunner 类的脚本。 有一些方法可以杀死任意运行的 Python 线程,但这些方法既不nice也不简单。您可以在此处找到此问题的一般情况的答案:»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 变量的值,如果为真则中断:
#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() ?后者似乎只管用。