python用多线程可以快几倍_在python中使用多线程时如何获得更快的速度

尽管David Beazley的演讲解释了网络流量改善了Python线程模块的调度,但是您应该使用multiprocessing module。我在你的代码中加入了这个选项(见我的答案的底部)。

在我的一台旧机器上运行(Python2.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

我同意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

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值