threads and queues

This commit is contained in:
hg
2015-06-09 22:09:28 +02:00
parent ea0c98b4c5
commit d851a9f5dc
4 changed files with 146 additions and 61 deletions

36
MongoWriter.py Normal file
View File

@ -0,0 +1,36 @@
'''
Created on 09.06.2015
@author: wn
'''
import threading
from logger import Logger
import pymongo
class MongoWriter(threading.Thread):
def __init__(self, queue, dbhost, database, collection):
threading.Thread.__init__(self)
self.queue = queue
self.dbhost = dbhost
self.database = database
self.collection = collection
self.setDaemon(True)
def run(self):
while True:
try:
msg = self.queue.get()
# Logger.debug("MongoWriter receives: %s" % msg)
mongoClient = pymongo.MongoClient(self.dbhost)
db = mongoClient[self.database]
res = db[self.collection].insert_one(msg)
Logger.debug("MongoWriter inserts: %s" % res.inserted_id)
except pymongo.errors.ServerSelectionTimeoutError, e:
Logger.log("Exception %s in MongoWriter, run" % str(e))
Logger.log("Msg dropped: %s" % msg)
except TypeError, e:
Logger.log("Exception %s in MongoWriter, run" % str(e))
except Exception, e:
Logger.log("Unexcepted exception %s in MongoWriter: %s" % (e.__class__.__name__, str(e)))