<p>在许多情况下,python的线程并不能很好地提高执行速度。。。有时候,这会让事情变得更糟。有关详细信息,请参见<a href="https://www.youtube.com/watch?v=Obt-vMVdM8s" rel="nofollow noreferrer">David Beazley's PyCon2010 presentation on the Global Interpreter Lock</a>/<a href="http://www.dabeaz.com/python/UnderstandingGIL.pdf" rel="nofollow noreferrer">Pycon2010 GIL slides</a>。这个演示内容非常丰富,我强烈推荐给任何考虑线程的人。。。</p>
<p>尽管David Beazley的演讲解释了网络流量改善了Python线程模块的调度,但是您应该使用<a href="http://docs.python.org/library/multiprocessing.html" rel="nofollow noreferrer">multiprocessing module</a>。我在你的代码中加入了这个选项(见我的答案的底部)。</p>
<p>在我的一台旧机器上运行(Python2.6.6):</p>
<pre><code>current_post.mode == "Process" (multiprocessing) --> 0.2609 seconds
current_post.mode == "Multiple" (threading) --> 0.3947 seconds
current_post.mode == "Simple" (serial execution) --> 1.650 seconds
</code></pre>
<p>我同意TokenMacGuy的评论,上面的数字包括将<code>.join()</code>移动到不同的循环。如您所见,python的多处理速度明显快于线程。</p>
<hr/>
<pre><code>from multiprocessing import Process
import threading
import time
import urllib
import urllib2
class Post:
def __init__(self, website, data, mode):
self.website = website
self.data = data
#mode is either:
# "Simple" (Simple POST)
# "Multiple" (Multi-thread POST)
# "Process" (Multiprocessing)
self.mode = mode
self.run_job()
def post(self):
#post data
req = urllib2.Request(self.website)
open_url = urllib2.urlopen(req, self.data)
if self.mode == "Multiple":
time.sleep(0.001)
#read HTMLData
HTMLData = open_url.read()
#print "OK"
def run_job(self):
"""This was refactored from the OP's code"""
origin_time = time.time()
if(self.mode == "Multiple"):
#multithreading POST
threads = list()
for i in range(0, 10):
thread = threading.Thread(target = self.post)
thread.start()
threads.append(thread)
for thread in threads:
thread.join()
#calculate the time interval
time_interval = time.time() - origin_time
print "mode - {0}: {1}".format(method, time_interval)
if(self.mode == "Process"):
#multiprocessing POST
processes = list()
for i in range(0, 10):
process = Process(target=self.post)
process.start()
processes.append(process)
for process in processes:
process.join()
#calculate the time interval
time_interval = time.time() - origin_time
print "mode - {0}: {1}".format(method, time_interval)
if(self.mode == "Simple"):
#simple POST
for i in range(0, 10):
self.post()
#calculate the time interval
time_interval = time.time() - origin_time
print "mode - {0}: {1}".format(method, time_interval)
return time_interval
if __name__ == "__main__":
for method in ["Process", "Multiple", "Simple"]:
Post("http://forum.xda-developers.com/login.php",
"vb_login_username=test&vb_login_password&securitytoken=guest&do=login",
method
)
</code></pre>