Terminate multiple threads when any thread completes a task

前端 未结 5 832
囚心锁ツ
囚心锁ツ 2020-11-30 19:33

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 thre

5条回答
  •  孤街浪徒
    2020-11-30 20:06

    Starting a thread is easy:

    thread = threading.Thread(function_to_call_inside_thread)
    thread.start()
    

    Create an event object to notify when you are done:

    event = threading.Event()
    event.wait() # call this in the main thread to wait for the event
    event.set() # call this in a thread when you are ready to stop
    

    Once the event has fired, you'll need to add stop() methods to your crawlers.

    for crawler in crawlers:
        crawler.stop()
    

    And then call join on the threads

    thread.join() # waits for the thread to finish
    

    If you do any amount of this kind of programming, you'll want to look at the eventlet module. It allows you to write "threaded" code without many of the disadvantages of threading.

提交回复
热议问题