/
DownloadFile.py
executable file
·50 lines (40 loc) · 1.77 KB
/
DownloadFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#encoding=utf-8
from log import logging
import threading
import os
class DownloadThread(threading.Thread):
session = ''
AllFiles = []
FileLock = threading.Lock()
def __init__(self):
threading.Thread.__init__(self)
def run(self):
logging.debug('==============start new thread to download file==================')
while True:
if self.FileLock.acquire():
if len(self.AllFiles) == 0:
self.FileLock.release()
break
fileInfo = self.AllFiles[0]
del self.AllFiles[0]
self.FileLock.release()
logging.debug('Thread[%s] start downloading another file[%s],left files[%d]' %(self.name, fileInfo['fileName'], len(self.AllFiles)))
self.DownloadFunc(fileInfo)
logging.debug("Thread[%s] exit" %(self.name))
def DownloadFunc(self, fileInfo):
filePath = 'fileDir/'+fileInfo['owner']
if os.path.exists(filePath+'/'+fileInfo['fileName']):
logging.debug("file[%s] has been downloaded" %fileInfo['fileName'])
return
try:
fileData = self.session.get(fileInfo['downloadPath'])
except Exception as e:
logging.error(e)
if self.FileLock.acquire() == True:
self.AllFiles.append(fileInfo)
self.FileLock.release()
return
with open(filePath+'/'+fileInfo['fileName'], "wb") as newfile:
newfile.write(fileData.content)
newfile.close()
logging.debug('===download [%s] end====' %(fileInfo['fileName']))