I am using requests to issue a get on a webpage where new data is added as events occur in the real world. I want to continue getting this data as long as the window is open so I set stream = True
and then iterate line-by-line over the data as it streams in.
page = requests.get(url, headers=headers, stream=True)
# Process the LiveLog data until stopped from exterior source
for html_line in page.iter_lines(chunk_size=1):
# Do other work here
I have no problem with this part, but when it comes to exiting this loop I run into a problem. From looking at other StackOverflow threads I understand I can't catch any signals since my for loop is blocking. Instead I've tried using the following code which does work but with one big problem.
if QThread.currentThread().isInterruptionRequested():
break
This code will get me out of my loop, but I've found that the only time the for loop iterates is when new data is introduced to the get, and in my situation this is not continuous. I could go without any new data for minutes or longer, and don't want to have to wait on this new data to land before I go through my loop again to check if an interruption is requested.
How can I exit my loop immediately after a user-action?
with
– Bencherwith
to solve this problem. – Populouswith
seems to be used as a method of cleaning up the get, but I don't think there's anything here about the problem of not being able to interrupt thefor
loop, especially if the data is not flowing continuously (making the for loop iterate continuously). – Populouslock = threading.Lock()
can help you or not. – Bencher