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

后端 未结 4 860
天命终不由人
天命终不由人 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:45

    Keep in mind that the only case where multi-threading can "increase speed" in Python is when you have operations like this one that are heavily I/O bound. Otherwise multi-threading does not increase "speed" since it can not run on more than one CPU (no, not even if you have multiple cores, python doesn't work that way). You should use multi-threading when you want two things to be done at the same time, not when you want two things to be parallel (i.e. two processes running separately).

    Now, what you're actually doing will not actually increase the speed of any single DNS lookup, but it will allow for multiple requests to be shot off while waiting for the results of some others, but you should be careful of how many you do or you will just make the response times even worse than they already are.

    Also please stop using urllib2, and use Requests: http://docs.python-requests.org

提交回复
热议问题