DataStore.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. else:
  9. from db.SqlHelper import SqlHelper as SqlHelper
  10. sqlhelper = SqlHelper()
  11. sqlhelper.init_db()
  12. except Exception,e:
  13. raise Con_DB_Fail
  14. def store_data(queue2,db_proxy_num):
  15. '''
  16. 读取队列中的数据,写入数据库中
  17. :param queue2:
  18. :return:
  19. '''
  20. successNum = 0
  21. failNum = 0
  22. while True:
  23. try:
  24. proxy = queue2.get(timeout=300)
  25. if proxy:
  26. sqlhelper.insert(proxy)
  27. successNum += 1
  28. else:
  29. failNum += 1
  30. str = u'IPProxyPool----->>>>>>>>Success ip num :%d,Fail ip num:%d' % (successNum,failNum)
  31. sys.stdout.write(str+"\r")
  32. sys.stdout.flush()
  33. except BaseException,e:
  34. if db_proxy_num.value != 0:
  35. successNum += db_proxy_num.value
  36. db_proxy_num.value=0
  37. str = u'IPProxyPool----->>>>>>>>Success ip num :%d,Fail ip num:%d' % (successNum,failNum)
  38. sys.stdout.write(str+"\r")
  39. sys.stdout.flush()
  40. successNum = 0
  41. failNum = 0