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 threadingimport timeurls = list(range(100))def p(url):    for u in url:        print(u)start = 0end = 20for i in range(5):    url_new = urls[start:end]    start += 20    end += 20    t = threading.Thread(target=p, args=(url_new,))    t.start()
相關文章
相關標籤/搜索