I am building an automation using python that requires tab switching, mouse clicking, etc which engages my system completely. During which I cannot access my terminal or taskbar to manually close/stop my program from running.
While the automation is running and something went wrong it is difficult to switch to terminal and press CTRL + C to stop executing since it collapses with the automation making it more of a mess.
I tries using pynput’s keyboard library since I am using it for my automation, but I can’t find a way to run both the scripts to run simultaneously.
Please help me how to stop my application without disturbing its flow through keyboard input like esc key, something like multithreading or any other way I can work with it, thank you.
import json
import automated as auto
# Read the data from the JSON file
with open('mouse.json', 'r') as f:
data = json.load(f)
# Convert lists back to tuples
account_location = [tuple(lst) for lst in data["account_location"]]
tab_location = [tuple(lst) for lst in data["tab_location"]]
auto.open_ms()
auto.full_screen()
auto.scroll_down()
auto.open_all_accounts(account_location)
for i in range(2): # 2 times for 8 accounts ( 4 accounts per time )
auto.position_tabs()
count = 0
auto.search_tabs(count, tab_location)
auto.exit_tabs()
auto.shutdown()
NOTE : I do not consider pasting the automated.py file since it’s just a bunch of loops and keyboard input in it. Just to be sure this code is not in loop.
A way/method to stop a python program from executing anytime without engaging with terminal.
You can solve this issue by using a combination of multithreading and a keyboard listener that will allow you to stop the script at any point without interfering with the automation flow. You can use pynput
library for keyboard monitoring in a separate thread, and stopping the main automation when a specific key(like ESC
) is pressed.
Here’s the code.
- First, you need to install
pynput
pip install pynput
- Modify your code to include multithreading and a keyboard listener.
import json
import automated as auto
from pynput import keyboard
import threading
import sys
# Flag to control stopping the automation
stop_flag = False
def on_press(key):
global stop_flag
try:
# If ESC is pressed, set the flag to True to stop the automation
if key == keyboard.Key.esc:
print("ESC pressed. Stopping automation...")
stop_flag = True
return False # Stop the listener
except Exception as e:
print(f"Error in listener: {e}")
def run_automation():
global stop_flag
# Read the data from the JSON file
with open('mouse.json', 'r') as f:
data = json.load(f)
# Convert lists back to tuples
account_location = [tuple(lst) for lst in data["account_location"]]
tab_location = [tuple(lst) for lst in data["tab_location"]]
auto.open_ms()
auto.full_screen()
auto.scroll_down()
auto.open_all_accounts(account_location)
for i in range(2): # 2 times for 8 accounts ( 4 accounts per time )
if stop_flag: # Check if stop is requested
print("Automation stopped.")
return
auto.position_tabs()
count = 0
auto.search_tabs(count, tab_location)
auto.exit_tabs()
auto.shutdown()
# Run the automation in a separate thread
automation_thread = threading.Thread(target=run_automation)
automation_thread.start()
# Start keyboard listener in the main thread
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
# Join automation thread after listener is done
automation_thread.join()
I hope this will help you a little.