# coding=utf-8
import os
import requests
import time
from concurrent import futures
from concurrent.futures import wait as fu_wait
import threading
gLock = threading.Lock()
gPool = futures.ThreadPoolExecutor(7)
def printInfo(txt):
global gLock
gLock.acquire()
print(txt)
gLock.release()
def downOne(url):
name = os.path.basename(url)
printInfo("downOne:start {}".format(name))
result = requests.get(url)
printInfo("downOne:middle {}".format(name))
if result.status_code == 200:
with open(name, "wb") as fp:
r = fp.write(result.content)
printInfo("downOne: r = {}".format(r))
printInfo("downOne:endxx {}".format(name))
return "ok {}".format(name)
def deal():
fus = []
global gPool
for i in range(1, 10):
name = str(i) + ".mp3"
path = "http://mp3-d.xxxx.com:9090/有声书/闪婚剩女" + "/" + name
fu = gPool.submit(downOne, path)
printInfo("name={}, fu = {}, done={}".format(name, fu, fu.done()))
fus.append(fu)
printInfo("deal:end {}".format(len(fus)))
return fus
if __name__ == "__main__":
start = time.time()
fs = deal()
printInfo("main middle: {}, {}".format(len(fs), time.time()-start))
printInfo("wait: {}".format(fu_wait(fs)))
while True:
ok = True
for f in fs:
if not f.done():
ok = False
if ok:
printInfo("main:ok {}".format(time.time()-start))
break
printInfo("main end {}".format(time.time()-start))
while True:
line = input("input:")
if line == 'q':
break
结果:
downOne:start 1.mp3
name=1.mp3, fu = <Future at 0x1a1d4a06d08 state=running>, done=False
downOne:start 2.mp3
name=2.mp3, fu = <Future at 0x1a1d469b808 state=running>, done=False
downOne:start 3.mp3
name=3.mp3, fu = <Future at 0x1a1d4a2e908 state=running>, done=False
downOne:start 4.mp3
name=4.mp3, fu = <Future at 0x1a1d4a40448 state=running>, done=False
downOne:start 5.mp3
name=5.mp3, fu = <Future at 0x1a1d4a4ba08 state=running>, done=False
downOne:start 6.mp3
name=6.mp3, fu = <Future at 0x1a1d4a51548 state=running>, done=False
downOne:start 7.mp3
name=7.mp3, fu = <Future at 0x1a1d4a8dc08 state=running>, done=False
name=8.mp3, fu = <Future at 0x1a1d4a9b188 state=pending>, done=False
name=9.mp3, fu = <Future at 0x1a1d4aa2d88 state=pending>, done=False
deal:end 9
main middle: 9, 0.09094810485839844
downOne:middle 2.mp3
downOne: r = 4953446
downOne:endxx 2.mp3
downOne:start 8.mp3
downOne:middle 3.mp3
downOne: r = 4826282
downOne:endxx 3.mp3
downOne:start 9.mp3
downOne:middle 4.mp3
downOne: r = 4911755
downOne:endxx 4.mp3
downOne:middle 7.mp3
downOne: r = 4941848
downOne:endxx 7.mp3
downOne:middle 6.mp3
downOne: r = 4956686
downOne:endxx 6.mp3
downOne:middle 1.mp3
downOne: r = 5231285
downOne:endxx 1.mp3
downOne:middle 5.mp3
downOne: r = 4837358
downOne:endxx 5.mp3
downOne:middle 9.mp3
downOne: r = 4909038
downOne:endxx 9.mp3
downOne:middle 8.mp3
downOne: r = 4995242
downOne:endxx 8.mp3
wait: DoneAndNotDoneFutures(done={<Future at 0x1a1d469b808 state=finished returned str>, <Future at 0x1a1d4a8dc08 state=finished returned str>, <Future at 0x1a1d4a4ba08 state=finished returned str>, <Future at 0x1a1d4a40448 state=finished returned str>, <Future at 0x1a1d4a2e908 state=finished returned str>, <Future at 0x1a1d4a06d08 state=finished returned str>, <Future at 0x1a1d4a9b188 state=finished returned str>, <Future at 0x1a1d4a51548 state=finished returned str>, <Future at 0x1a1d4aa2d88 state=finished returned str>}, not_done=set())
main:ok 80.31756949424744
main end 80.3175694942474