-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreads.py
212 lines (142 loc) · 4.58 KB
/
threads.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#-*- coding:utf-8 -*-
import threading
import time
import os
import urllib2
class ThreadList(object):
__tlist={}
__index=-1
__fail_data=[]
def __init__(self,num,threadtype,feed):
self.__max=num
self.__type=threadtype
self.__feed=feed
#lock of the threadlist
self.__list_lock=threading.Lock()
#lock of the feeder
self.__feed_condition=feed.get_condition()
for i in xrange(num):
tmpthread=self.__type()
self._add(tmpthread)
pass
def _get_feed(self):
#TODO add lock
one=self.__feed.get_one()
self.__feed_condition.acquire()
while one==None:
self.__feed_condition.wait()
print "waiting"
one=self.__feed.get_one()
self.__feed_condition.release()
return one
def _get_list(self):
return self.__tlist
def _add(self,thread):
#acquire
self.__list_lock.acquire()
self.__index+=1
#print "index is %d" %self.__index
thread._reg(self,self.__index)
thread.start()
self.__tlist.update({self.__index:thread})
#release
self.__list_lock.release()
one =self._get_feed()
thread._feed(one)
return self.__index
def _del(self,thread):
#thread.join()
tid=thread._get_id()
self.__list_lock.acquire()
del self.__tlist[tid]
self.__list_lock.release()
def _worker_dead(self,worker):
self._del(worker)
#self.__list_lock.acquire()
#if len(self.__tlist)<self.__max:
self._add(self.__type())
#self.__list_lock.release()
def _worker_fail(self,worker,data):
self._worker_dead(worker)
self.__fail_data.append(data)
print "fail link : ",data
def _job_finished(self,worker):
one=self._get_feed()
worker._feed(one)
def _get_length(self):
#TODO lock
return len(self.__tlist)
class UrlDirName(threading.Thread):
__id=''
__feed=''
__tlist=''
__try_max=10
__try_time=0
def __init__(self):
threading.Thread.__init__(self)
self.__stop=threading.Event()
self.daemon=True
def run(self):
while True:
if self.is_stop():
print "thread %d is stoped" %self.__id
#if self.__tlist!='':
# self.__tlist._worker_dead(self)
#self.join()
return
while self.__feed=='':
print "thread %d is waiting to be feed..." % self.__id
time.sleep(2)
try:
line =self.__feed
self.__feed=''
url,directory,name=line.split(',')
directory='tmp/'+directory
path=directory+'/'+name+url.split('/')[-1]
except:
print "broken url"
self.__tlist._worker_fail(self,"broken url")
self.stop()
continue
if os.path.exists(path):
print "duplicate file",path
#path+="1"
self.__tlist._worker_fail(self,url)
self.stop()
continue
print "starting to download ",url
tmpfile=self.__download(url)
if tmpfile==None:
self.__tlist._worker_fail(self,url)
self.stop()
continue
print url,"downloaded"
if os.path.exists(directory) is False:
os.makedirs(directory)
try:
tmpfp=open(path,'wb')
tmpfp.write(tmpfile)
tmpfp.close()
except:
self.__tlist._worker_fail(self,url)
self.stop()
continue
self.__tlist._job_finished(self)
def _get_id(self):
return self.__id
def _feed(self,data):
self.__feed=data
def _reg(self,tlist,tid):
self.__tlist=tlist
self.__id=tid
def stop(self):
self.__stop.set()
def is_stop(self):
return self.__stop.is_set()
def __download(self,url):
for i in xrange(self.__try_max):
try:
return urllib2.urlopen(url).read()
except:
pass
return None