Sea*_*ene 8 python post multithreading tcp
现在我正在研究如何尽快从网站上获取数据.为了获得更快的速度,我考虑使用多线程.这是我用来测试多线程和简单帖子之间差异的代码.
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) or "Multiple"(Multi-thread POST)
self.mode = mode
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"
if __name__ == "__main__":
current_post = Post("http://forum.xda-developers.com/login.php", "vb_login_username=test&vb_login_password&securitytoken=guest&do=login", \
"Simple")
#save the time before post data
origin_time = time.time()
if(current_post.mode == "Multiple"):
#multithreading POST
for i in range(0, 10):
thread = threading.Thread(target = current_post.post)
thread.start()
thread.join()
#calculate the time interval
time_interval = time.time() - origin_time
print time_interval
if(current_post.mode == "Simple"):
#simple POST
for i in range(0, 10):
current_post.post()
#calculate the time interval
time_interval = time.time() - origin_time
print time_interval
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,这是一个非常简单的代码.首先我将模式设置为"简单",我可以得到时间间隔:50秒(也许我的速度有点慢:().然后我将模式设置为"多个",我得到时间间隔:35.从我可以看出,多线程实际上可以提高速度,但结果并不像我想象的那样好.我希望得到更快的速度.
从调试开始,我发现该程序主要阻塞在以下行:open_url = urllib2.urlopen(req, self.data)
这行代码需要花费大量时间来发布和接收来自指定网站的数据.我想也许我可以通过time.sleep()
在urlopen
函数内部添加和使用多线程来获得更快的速度,但我不能这样做,因为它是python自己的函数.
如果不考虑服务器阻止后期速度的可能限制,我还能做些什么才能获得更快的速度?或者我可以修改的任何其他代码?多谢!
你做错的最重要的事情就是你打电话的方式thread.start()
和thread.join()
:
for i in range(0, 10):
thread = threading.Thread(target = current_post.post)
thread.start()
thread.join()
Run Code Online (Sandbox Code Playgroud)
每次循环,你创建一个线程,启动它,然后等待它完成继续前进到下一个线程.你根本没有做任何事情!
你可能应该做的是:
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()
Run Code Online (Sandbox Code Playgroud)
在许多情况下,python的线程并不能很好地提高执行速度......有时,它会使情况变得更糟.有关更多信息,请参阅David Beazley关于Global Interpreter Lock/Pycon2010 GIL幻灯片的PyCon2010演示文稿.这个演示文稿内容丰富,我强烈推荐给任何考虑线程的人...
尽管David Beazley的演讲解释了网络流量改进了Python线程模块的调度,但您应该使用多处理模块.我在您的代码中包含了这个选项(请参阅我的答案底部).
在我的一台旧机器上运行它(Python 2.6.6):
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
Run Code Online (Sandbox Code Playgroud)
我同意TokenMacGuy的评论,上面的数字包括转移.join()
到另一个循环.如您所见,python的多处理速度明显快于线程.
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
)
Run Code Online (Sandbox Code Playgroud)