Mar-16-2024, 06:37 PM
Hi all,
I am using the threading module to run a while loop as a thread which scrapes stock information off of Yahoo Finance and appends it to a csv file parallel to functions which plot this information on a graph.
The issue is, the user should be able to leave the graph page (in which the thread stops doing what it's doing) to access other parts of the program and then potentially enter this section again and the thread should restart (not continue from where it left off).
My initial thought was to just kill the thread and create it again but this doesn't work as threads can only be started once and cannot be killed.
I have seen online that it may be possible to create another instance of the thread and start this but I don't believe this will work in the context of my project as this would mean I would have to iterate variable names and start new threads each time which is one, outside my level of expertise and two, not very practical.
Basically, I was wondering if there is a way to pause a thread or 'kill' it to stop what it's doing and then restart it later on if called again. I have read online about threading events but they don't make much sense to me. I have tried to use a flagging variable which is set to False initially and changes to True when I want the thread to stop but this doesn't actually kill the thread and instead just runs a while loop in the background with no output and just slows down the entire program.
I am using the threading module to run a while loop as a thread which scrapes stock information off of Yahoo Finance and appends it to a csv file parallel to functions which plot this information on a graph.
The issue is, the user should be able to leave the graph page (in which the thread stops doing what it's doing) to access other parts of the program and then potentially enter this section again and the thread should restart (not continue from where it left off).
My initial thought was to just kill the thread and create it again but this doesn't work as threads can only be started once and cannot be killed.
I have seen online that it may be possible to create another instance of the thread and start this but I don't believe this will work in the context of my project as this would mean I would have to iterate variable names and start new threads each time which is one, outside my level of expertise and two, not very practical.
Basically, I was wondering if there is a way to pause a thread or 'kill' it to stop what it's doing and then restart it later on if called again. I have read online about threading events but they don't make much sense to me. I have tried to use a flagging variable which is set to False initially and changes to True when I want the thread to stop but this doesn't actually kill the thread and instead just runs a while loop in the background with no output and just slows down the entire program.
stock_thread.start() # Initially start the thread
def goMenu(self): stop_thread = True
stop_thread = False # To continue the thread
while len(stock_symbol) > 0: if not stop_thread: #code that does what i mentionedI am fairly new to programming and any help would be much appreciated
