Python – Linux – Terminates AutoKey scripts using bind

Linux – Terminates AutoKey scripts using bind… here is a solution to the problem.

Linux – Terminates AutoKey scripts using bind

Well, I’m

new to the AutoKey app on elementaryOS devices and I’m just playing around with some custom scripts.

I do find strange that there is no simple option to terminate a running script.

Well, is there a nice and easy way to achieve this.

Please forgive my incompetence

Solution

There is currently no such method.

Autokey uses a simple mechanism to run scripts at the same time: each script is executed in a separate Python thread. It uses this Wrapper use ScriptRunner Run the script class.
There are ways to kill arbitrarily running Python threads, but these methods are neither good nor simple. You can find the answer to the general case of this question here:» Is there any way to kill a Thread in Python? «

There is a nice possibility, but it is not really simple and requires the support of your script. You can use the global script store to send « Stop signals to the script». API documentation can be found here :

Suppose, this is the script you want to break:

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

Bind such a stop script binding (bind) to the hotkey:

store.set_global_value("STOP", True)

Then modify your script to poll for the value of the STOP variable and break if it is 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()

You should add such a stop check for every hot or long-running code path.
This won’t help if you get deadlocks in your script.

Related Problems and Solutions