Skip to content

Commit 0e079b4

Browse files
authored
Merge pull request jhao104#131 from highroom/branch1
modify proxy valid use queue
2 parents b82118f + 0c38690 commit 0e079b4

File tree

2 files changed

+47
-26
lines changed

2 files changed

+47
-26
lines changed

Schedule/ProxyCheck.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
__author__ = 'J_hao'
1414

1515
import sys
16-
from time import sleep
16+
import threading
1717
from threading import Thread
1818

1919
sys.path.append('../')
@@ -26,32 +26,36 @@
2626

2727

2828
class ProxyCheck(ProxyManager, Thread):
29-
def __init__(self):
29+
def __init__(self, queue, item_dict):
3030
ProxyManager.__init__(self)
3131
Thread.__init__(self)
3232
self.log = LogHandler('proxy_check')
33+
self.queue = queue
34+
self.item_dict = item_dict
3335

3436
def run(self):
3537
self.db.changeTable(self.useful_proxy_queue)
36-
while True:
37-
for proxy, count in self.db.getAll().items():
38-
if validUsefulProxy(proxy):
39-
# 验证通过计数器减1
40-
if count and int(count) > 0:
41-
self.db.put(proxy, num=int(count) - 1)
42-
else:
43-
pass
44-
self.log.info('ProxyCheck: {} validation pass'.format(proxy))
38+
while self.queue.qsize():
39+
proxy = self.queue.get()
40+
count = self.item_dict[proxy]
41+
if validUsefulProxy(proxy):
42+
# 验证通过计数器减1
43+
if count and int(count) > 0:
44+
self.db.put(proxy, num=int(count) - 1)
4545
else:
46-
self.log.info('ProxyCheck: {} validation fail'.format(proxy))
47-
if count and int(count) > FAIL_COUNT:
48-
self.log.info('ProxyCheck: {} fail too many, delete!'.format(proxy))
49-
self.db.delete(proxy)
50-
else:
51-
self.db.put(proxy, num=int(count) + 1)
52-
sleep(60 * 5)
46+
pass
47+
print('ProxyCheck: {} validation pass'.format(proxy))
48+
else:
49+
print('ProxyCheck: {} validation fail'.format(proxy))
50+
if count and int(count) > FAIL_COUNT:
51+
print('ProxyCheck: {} fail too many, delete!'.format(proxy))
52+
self.db.delete(proxy)
53+
else:
54+
self.db.put(proxy, num=int(count) + 1)
55+
self.queue.task_done()
5356

5457

5558
if __name__ == '__main__':
56-
p = ProxyCheck()
57-
p.run()
59+
# p = ProxyCheck()
60+
# p.run()
61+
pass

Schedule/ProxyValidSchedule.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,25 @@
1717
sys.path.append('../')
1818

1919
from Schedule.ProxyCheck import ProxyCheck
20+
from Manager.ProxyManager import ProxyManager
21+
from queue import Queue
22+
import time
2023

2124

22-
class ProxyValidSchedule(object):
25+
class ProxyValidSchedule(ProxyManager, object):
2326
def __init__(self):
24-
pass
27+
ProxyManager.__init__(self)
28+
self.queue = Queue()
2529

26-
def __validProxy(self, threads=5):
30+
def __validProxy(self, threads=10):
2731
"""
2832
验证useful_proxy代理
2933
:param threads: 线程数
3034
:return:
3135
"""
3236
thread_list = list()
3337
for index in range(threads):
34-
thread_list.append(ProxyCheck())
38+
thread_list.append(ProxyCheck(self.queue, self.item_dict))
3539

3640
for thread in thread_list:
3741
thread.daemon = True
@@ -41,7 +45,20 @@ def __validProxy(self, threads=5):
4145
thread.join()
4246

4347
def main(self):
44-
self.__validProxy()
48+
self.put_queue()
49+
while True:
50+
if self.queue.qsize():
51+
self.__validProxy()
52+
else:
53+
print('Time sleep 5 minutes.')
54+
time.sleep(60 * 1)
55+
self.put_queue()
56+
57+
def put_queue(self):
58+
self.db.changeTable(self.useful_proxy_queue)
59+
self.item_dict = self.db.getAll()
60+
for item in self.item_dict:
61+
self.queue.put(item)
4562

4663

4764
def run():
@@ -51,4 +68,4 @@ def run():
5168

5269
if __name__ == '__main__':
5370
p = ProxyValidSchedule()
54-
p.main()
71+
p.main()

0 commit comments

Comments
 (0)