I am new to both python, and to threads. I have written python code which acts as a web crawler and searches sites for a specific keyword. My question is, how can I use threads to run three different instances of my class at the same time. When one of the instances finds the keyword, all three must close and stop crawling the web. Here is some code.
class Crawler:
def __init__(self):
# the actual code for finding the keyword
def main():
Crawl = Crawler()
if __name__ == "__main__":
main()
How can I use threads to have Crawler do three different crawls at the same time?
There doesn’t seem to be a (simple) way to terminate a thread in Python.
Here is a simple example of running multiple HTTP requests in parallel:
With additional overhead, you can use a multi-process aproach that’s more powerful and allows you to terminate thread-like processes.
I’ve extended the example to use that. I hope this will be helpful to you: