multithreading - How to terminate thread in python to jump out of loop and then continue the loop again? -
I wrote a piece of code for scrapping in Python. I have a list of urls, which is to scrap, but after reading the web pages in the loop the script is lost sometime. So I have to set a certain time, after which the script should come out of the loop and start reading the next web page.
Below is sample code.
def (main): If & lt; Some condition & gt; list_of_links = ['http://link1.com', 'http://link2.com', 'http://link3.com'] for link in list_of_links: Process (link) DEF Process (): & lt; Some code for reading web pages & gt; Return page_read The script is lost inside the process (), which is called repeated for the loop, I want to go to the next link for the loop if the process () method Taking more that one-minute webpage to read.
You probably can use one depending on the code inside your process function. If your main and process work are one class methods, then:
class MyClass: def __init __ (self): self.stop_thread = incorrect DEF main (): if & lt; Some condition & gt; list_of_links = ['http://link1.com', 'http://link2.com', 'http://link3.com'] for link in list_of_links: Process (link) def set_stop (self): auto stop_thread = true def process (): t = timer (60.0, auto .set_stop) t.start () # I do not know my code here # if you use any type of loop then it can be Is: while correct: # do something .. if self.stop_thread: break # or: if self.stop_thread: return
Comments
Post a Comment