Python Forum
Having difficulty with threads and input()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having difficulty with threads and input()
#1
Hello,

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.
Reply


Messages In This Thread
Having difficulty with threads and input() - by sawtooth500 - Jun-03-2024, 07:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Difficulty in adapting duplicates-filter in script ledgreve 5 1,087 Jul-17-2023, 03:46 PM
Last Post: ledgreve
  Difficulty with installation standenman 2 1,170 May-03-2023, 06:39 PM
Last Post: snippsat
  Difficulty with installation standenman 0 795 May-02-2023, 08:33 PM
Last Post: standenman
  Difficulty in understanding transpose with a tuple of axis numbers in 3-D new_to_python 0 1,625 Feb-11-2020, 06:03 AM
Last Post: new_to_python
  Difficulty installing Pycrypto KipCarter 4 13,282 Feb-10-2020, 07:54 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020