-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathDataStore.py
More file actions
55 lines (46 loc) · 1.56 KB
/
DataStore.py
File metadata and controls
55 lines (46 loc) · 1.56 KB
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
# coding:utf-8
import sys,os
BASE_DIR = os.path.dirname(os.path.dirname((os.path.abspath(__file__))))
if BASE_DIR not in sys.path:
sys.path.append(BASE_DIR)
from config import DB_CONFIG
from util.exception import Con_DB_Fail
try:
if DB_CONFIG['DB_CONNECT_TYPE'] == 'pymongo':
from db.MongoHelper import MongoHelper as SqlHelper
elif DB_CONFIG['DB_CONNECT_TYPE'] == 'redis':
from db.RedisHelper import RedisHelper as SqlHelper
else:
from db.SqlHelper import SqlHelper as SqlHelper
sqlhelper = SqlHelper()
sqlhelper.init_db()
except Exception as e:
raise Con_DB_Fail
def store_data(queue2, db_proxy_num):
'''
读取队列中的数据,写入数据库中
:param queue2:
:return:
'''
successNum = 0
failNum = 0
while True:
try:
proxy = queue2.get(timeout=300)
if proxy:
sqlhelper.insert(proxy)
successNum += 1
else:
failNum += 1
str = 'IPProxyPool----->>>>>>>>Success ip num :%d,Fail ip num:%d' % (successNum, failNum)
sys.stdout.write(str + "\r")
sys.stdout.flush()
except BaseException as e:
if db_proxy_num.value != 0:
successNum += db_proxy_num.value
db_proxy_num.value = 0
str = 'IPProxyPool----->>>>>>>>Success ip num :%d,Fail ip num:%d' % (successNum, failNum)
sys.stdout.write(str + "\r")
sys.stdout.flush()
successNum = 0
failNum = 0