python多线程并发学习

1.python多线程适用于什么场景?
举个��:当我们想从网页上下载信息,或者从ftp服务器上下载版本时,若是版本太大,那么顺序的执行下载N个(N>1)版本就会耗费许多时间,若是可以并发地下载,那么就会省时很多
适用场景:IO密集型的场景
不适用场景:CPU使用率高的场景,如有大量数据的计算的场合,曾经用python处理过4000多个性能计算器生成的csv文件,发现多线程处理时,效率提升不明显。

2.怎么在代码中实现多线程并发?

a. 设置并发线程

#!/usr/bin/python env
# -*- coding:utf-8 -*-
import threading
import urllib2
import  time

def surf_net(url):
    start_time = time.time()
    print 'surf start', start_time
    req = urllib2.Request(url)

    try:
        urllib2.urlopen(req)
    except urllib2.URLError as e:
        print e.reason
    time.sleep(2)
    end_time = time.time()
    print url, urllib2.urlopen(req).code, end_time - start_time

url_list = ['https://www.taobao.com', 'https://www.baidu.com', 'https://www.jd.com']

for j in url_list:
    print j
    surf_net(j)

begin_time = time.time()
threads = []

for index in url_list:
    one_thread = threading.Thread(target=surf_net(index), args=(url_list[index],))
    threads.append(one_thread)

for j in threads:
    j.start()

for j in threads:
    j.join()

stop_time = time.time()

print 'bein to stop time is:', stop_time - begin_time

执行结果如下:

https://www.taobao.com
surf start 1501195294.39
https://www.taobao.com 200 3.3974211216
https://www.baidu.com
surf start 1501195297.99
https://www.baidu.com 200 0.184926986694
https://www.jd.com
surf start 1501195298.37
https://www.jd.com 200 0.247194051743
one by one run time is: 4.43907999992
surf start 1501195298.83
surf start 1501195298.83
surf start 1501195298.84
https://www.baidu.com https://www.jd.com 200 0.227499008179
200 0.272459030151
https://www.taobao.com 200 0.676004886627
bein to stop time is: 0.897297859192

上面的代码要访问3个网页,当顺序执行时,用的时间是4.43秒,但是若是并发执行,则用的是0.90秒。

你可能感兴趣的:(技术干货)