Python--多线程

import threading
import time
import requests


def say_hi(name):
time.sleep(2)
print(name)


def down_html(url1, name):
res = requests.get(url1).content
with open(name + '.html', 'wb') as f:
f.write(res)


urls = [
['nnzhp', 'http://www.nnzhp.cn'],
['dsx', 'http://www.imdsx.cn'],
['besttest', 'http://www.besttest.cn']
]

start_time = time.time()
# for url in urls:
# down_html(url[1], url[0])

threads = [] # 存放刚才启动的线程
for url in urls:
t = threading.Thread(target=down_html, args=(url[1], url[0]))
t.start()
threads.append(t)
for t in threads:
t.join() # 主线程等待子线程结束
end_time = time.time()
print(end_time - start_time)

# for i in range(10):
# t = threading.Thread(target=say_hi, args=('小黑',)) # 启动线程
# t.start()

100个url,启动5个线程
import threading
import time

urls = list(range(100))


def p(url):
for u in url:
print(u)


start = 0
end = 20

for i in range(5):
url_new = urls[start:end]
start += 20
end += 20
t = threading.Thread(target=p, args=(url_new,))
t.start()
posted @ 2018-03-07 15:16  王思磊  阅读(115)  评论(0编辑  收藏  举报