怎样加快python官网上应用的下载速度?

Python015

怎样加快python官网上应用的下载速度?,第1张

Python是一个很酷的语言,因为你可以在很短的时间内利用很少的代码做很多事情。不仅如此,它还能轻松地支持多任务,比如多进程等。Python批评者有时会说Python执行缓慢。本文将尝试介绍6个技巧,可加速你的Python应用程序。

1.让关键代码依赖于外部包

虽然Python让许多编程任务变得容易,但它可能并不总能为紧急的任务提供最佳性能。你可以为紧急的任务使用C、C++或机器语言编写的外部包,这样可以提高应用程序的性能。这些包都是不能跨平台的,这意味着你需要根据你正在使用的平台,寻找合适的包。简而言之,这个方案放弃了一些应用程序的可移植性,以换取只有在特定主机上直接编程才能获得的程序性能。这里有一些你应该考虑加入到你的“性能兵工厂”的包:

python是一款应用非常广泛的脚本程序语言,谷歌公司的网页就是用python编写。python在生物信息、统计、网页制作、计算等多个领域都体现出了强大的功能。python和其他脚本语言如java、R、Perl 一样,都可以直接在命令行里运行脚本程序。工具/原料

python;CMD命令行;windows操作系统

方法/步骤

1、首先下载安装python,建议安装2.7版本以上,3.0版本以下,由于3.0版本以上不向下兼容,体验较差。

2、打开文本编辑器,推荐editplus,notepad等,将文件保存成 .py格式,editplus和notepad支持识别python语法。

脚本第一行一定要写上 #!usr/bin/python

表示该脚本文件是可执行python脚本

如果python目录不在usr/bin目录下,则替换成当前python执行程序的目录。

3、编写完脚本之后注意调试、可以直接用editplus调试。调试方法可自行百度。脚本写完之后,打开CMD命令行,前提是python 已经被加入到环境变量中,如果没有加入到环境变量,请百度

4、在CMD命令行中,输入 “python” + “空格”,即 ”python “;将已经写好的脚本文件拖拽到当前光标位置,然后敲回车运行即可。

由于python的多线程中存在PIL锁,因此python的多线程不能利用多核,那么,由于现在的计算机是多核的,就不能充分利用计算机的多核资源。但是python中的多进程是可以跑在不同的cpu上的。因此,尝试了多进程+多线程的方式,来做一个任务。比如:从中科大的镜像源中下载多个rpm包。

#!/usr/bin/pythonimport reimport commandsimport timeimport multiprocessingimport threadingdef download_image(url):

print '*****the %s rpm begin to download *******' % url

commands.getoutput('wget %s' % url)def get_rpm_url_list(url):

commands.getoutput('wget %s' % url)

rpm_info_str = open('index.html').read()

regu_mate = '(?<=<a href=")(.*?)(?=">)'

rpm_list = re.findall(regu_mate, rpm_info_str)

rpm_url_list = [url + rpm_name for rpm_name in rpm_list]print 'the count of rpm list is: ', len(rpm_url_list)return rpm_url_list123456789101112131415161718192021

def multi_thread(rpm_url_list):

threads = []# url = 'https://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/'

# rpm_url_list = get_rpm_url_list(url)

for index in range(len(rpm_url_list)):print 'rpm_url is:', rpm_url_list[index]

one_thread = threading.Thread(target=download_image, args=(rpm_url_list[index],))

threads.append(one_thread)

thread_num = 5 # set threading pool, you have put 4 threads in it

while 1:

count = min(thread_num, len(threads))print '**********count*********', count ###25,25,...6707%25

res = []for index in range(count):

x = threads.pop()

res.append(x)for thread_index in res:

thread_index.start()for j in res:

j.join()if not threads:break1234567891011121314151617181920212223242526

def multi_process(rpm_url_list):

# process num at the same time is 4

process = []

rpm_url_group_0 = []

rpm_url_group_1 = []

rpm_url_group_2 = []

rpm_url_group_3 = []for index in range(len(rpm_url_list)):if index % 4 == 0:

rpm_url_group_0.append(rpm_url_list[index])elif index % 4 == 1:

rpm_url_group_1.append(rpm_url_list[index])elif index % 4 == 2:

rpm_url_group_2.append(rpm_url_list[index])elif index % 4 == 3:

rpm_url_group_3.append(rpm_url_list[index])

rpm_url_groups = [rpm_url_group_0, rpm_url_group_1, rpm_url_group_2, rpm_url_group_3]for each_rpm_group in rpm_url_groups:

each_process = multiprocessing.Process(target = multi_thread, args = (each_rpm_group,))

process.append(each_process)for one_process in process:

one_process.start()for one_process in process:

one_process.join()# for each_url in rpm_url_list:# print '*****the %s rpm begin to download *******' %each_url## commands.getoutput('wget %s' %each_url)123456789101112131415161718192021222324252627282930313233

def main():

url = 'https://mirrors.ustc.edu.cn/centos/7/os/x86_64/Packages/'

url_paas = 'http://mirrors.ustc.edu.cn/centos/7.3.1611/paas/x86_64/openshift-origin/'

url_paas2 ='http://mirrors.ustc.edu.cn/fedora/development/26/Server/x86_64/os/Packages/u/'

start_time = time.time()

rpm_list = get_rpm_url_list(url_paas)print multi_process(rpm_list)# print multi_thread(rpm_list)

#print multi_process()

# print multi_thread(rpm_list)

# for index in range(len(rpm_list)):

# print 'rpm_url is:', rpm_list[index]

end_time = time.time()print 'the download time is:', end_time - start_timeprint main()123456789101112131415161718

代码的功能主要是这样的:

main()方法中调用get_rpm_url_list(base_url)方法,获取要下载的每个rpm包的具体的url地址。其中base_url即中科大基础的镜像源的地址,比如:http://mirrors.ustc.edu.cn/centos/7.3.1611/paas/x86_64/openshift-origin/,这个地址下有几十个rpm包,get_rpm_url_list方法将每个rpm包的url地址拼出来并返回。

multi_process(rpm_url_list)启动多进程方法,在该方法中,会调用多线程方法。该方法启动4个多进程,将上面方法得到的rpm包的url地址进行分组,分成4组,然后每一个组中的rpm包再最后由不同的线程去执行。从而达到了多进程+多线程的配合使用。

代码还有需要改进的地方,比如多进程启动的进程个数和rpm包的url地址分组是硬编码,这个还需要改进,毕竟,不同的机器,适合同时启动的进程个数是不同的。