Jun-03-2024, 07:34 PM
Hello,
I have the below function running on it's own thread:
The purpose of this thread is so that at any point I can type 'x' then enter and it gracefully terminates the script. Sometimes, however, I will want the script to stop automatically without my user input, that's why I have the stop_event flag. Specifically, I call:
The problem, to my best guess, is that the input() function, while waiting for input, precludes the while loop from cycling and thereby checking stop_event. I've tried modifying function handle_input with an added parameter so that I can independently call the function without using the while loop to do cleanup actions, but that still doesn't stop the open thread. I've gotten it working where it automatically stops, but when that worked the manual stop no longer worked... When calling the function independently I tried also using an exit() to just kill the script but that resorted in various infinite loops for me...
I'm kind of out of ideas short of using ctypes to hard kill the thread... which I'd rather avoid but I can try barring other ideas. However, if anyone has any ideas how I can overcome this I'd appreciate it, thanks.
I have the below function running on it's own thread:
def handle_input(ws, stop_event): while not stop_event.is_set(): if input("Press 'x' key then enter to exit...\n") == 'x': break #Close out any open orders open_indices = orders_df[orders_df['Status'] == 'Open'].index.tolist() if open_indices: for ordernum in open_indices: cancel_order(ordernum) if enter_positions and position: placeorder(symbol, shares_str, 'market', 'exit') global keep_running, shutdown_flag print('$Rolling Profit = ', round(rolling_profit + (profit * shares), 3)) keep_running = False shutdown_flag = True ws.close()The thread is called with:
# Thread for handling user input stop_event = threading.Event() input_thread = threading.Thread(target=handle_input, args=(ws, stop_event)) input_thread.start()and continually runs from when I start the script.
The purpose of this thread is so that at any point I can type 'x' then enter and it gracefully terminates the script. Sometimes, however, I will want the script to stop automatically without my user input, that's why I have the stop_event flag. Specifically, I call:
stop_event.set()From within another function when I want the script to automatically and gracefully exit.
The problem, to my best guess, is that the input() function, while waiting for input, precludes the while loop from cycling and thereby checking stop_event. I've tried modifying function handle_input with an added parameter so that I can independently call the function without using the while loop to do cleanup actions, but that still doesn't stop the open thread. I've gotten it working where it automatically stops, but when that worked the manual stop no longer worked... When calling the function independently I tried also using an exit() to just kill the script but that resorted in various infinite loops for me...
I'm kind of out of ideas short of using ctypes to hard kill the thread... which I'd rather avoid but I can try barring other ideas. However, if anyone has any ideas how I can overcome this I'd appreciate it, thanks.