DataStore.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # coding:utf-8
  2. import sys
  3. from config import DB_CONFIG
  4. from util.exception import Con_DB_Fail
  5. try:
  6. if DB_CONFIG['DB_CONNECT_TYPE'] == 'pymongo':
  7. from db.MongoHelper import MongoHelper as SqlHelper
  8. elif DB_CONFIG['DB_CONNECT_TYPE'] == 'redis':
  9. from db.RedisHelper import RedisHelper as SqlHelper
  10. else:
  11. from db.SqlHelper import SqlHelper as SqlHelper
  12. sqlhelper = SqlHelper()
  13. sqlhelper.init_db()
  14. except Exception as e:
  15. raise Con_DB_Fail
  16. def store_data(queue2, db_proxy_num):
  17. '''
  18. 读取队列中的数据,写入数据库中
  19. :param queue2:
  20. :return:
  21. '''
  22. successNum = 0
  23. failNum = 0
  24. while True:
  25. try:
  26. proxy = queue2.get(timeout=300)
  27. if proxy:
  28. sqlhelper.insert(proxy)
  29. successNum += 1
  30. else:
  31. failNum += 1
  32. str = 'IPProxyPool----->>>>>>>>Success ip num :%d,Fail ip num:%d' % (successNum, failNum)
  33. sys.stdout.write(str + "\r")
  34. sys.stdout.flush()
  35. except BaseException as e:
  36. if db_proxy_num.value != 0:
  37. successNum += db_proxy_num.value
  38. db_proxy_num.value = 0
  39. str = 'IPProxyPool----->>>>>>>>Success ip num :%d,Fail ip num:%d' % (successNum, failNum)
  40. sys.stdout.write(str + "\r")
  41. sys.stdout.flush()
  42. successNum = 0
  43. failNum = 0