How to get a faster speed when using multi-threading in python

后端 未结 4 861
天命终不由人
天命终不由人 2020-12-01 15:00

Now i am studying how to fetch data from website as fast as possible. To get faster speed, im considering using multi-thread. Here is the code i used to test the difference

4条回答
  •  醉酒成梦
    2020-12-01 15:43

    The biggest thing you are doing wrong, that is hurting your throughput the most, is the way you are calling thread.start() and thread.join():

    for i in range(0, 10):
       thread = threading.Thread(target = current_post.post)
       thread.start()
       thread.join()
    

    Each time through the loop, you create a thread, start it, and then wait for it to finish Before moving on to the next thread. You aren't doing anything concurrently at all!

    What you should probably be doing instead is:

    threads = []
    
    # start all of the threads
    for i in range(0, 10):
       thread = threading.Thread(target = current_post.post)
       thread.start()
       threads.append(thread)
    
    # now wait for them all to finish
    for thread in threads:
       thread.join()
    

提交回复
热议问题