from threading import Thread

import zmq
import MySQLdb
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy import Column, Integer, Float, DateTime


class Writer(Thread):
    def __init__(self, context, polltimeout=100):
        super().__init__()

        self.subscriber = context.socket(zmq.SUB)
        self.subscriber.connect('inproc://dbwrite')
        self.subscriber.setsockopt_string(zmq.SUBSCRIBE, '')
        self.poller = zmq.Poller()
        self.poller.register(self.subscriber, zmq.POLLIN)

        self.db_uri = 'mysql+mysqldb://cypress:cypress@localhost:3306/sensors'

    def run(self):
        engine = create_engine(self.db_uri, echo=True)
        session = sessionmaker(bind=engine)()
        Base.metadata.create_all(engine)

        while True:
            events = dict(self.poller.poll(100))
            if not events:
                continue
            for socket in events:
                if events[socket] != zmq.POLLIN:
                    continue
                message = socket.recv_pyobj()
                self.store(session, message)
            session.commit()
        session.close()

    def store(self, session, message):
        identifier, datetime, data = message
        # DB storage
        if identifier == 'TEMP':
            session.add(Temperature(time=datetime,
                                    temperature=data))


Base = declarative_base()
class Sensor():

    id = Column(Integer, primary_key=True)
    time = Column(DateTime)

    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()


class Temperature(Sensor, Base):
    temperature = Column(Float)

    def __repr__(self):
        return "<T(time='%s', temperature='%s')>" % (
            self.time, self.temperature)

@@ -1,12 +1,24 @@
+import time%0A
 from threadi
@@ -374,16 +374,37 @@
 eout=100
+, commit_frequency=2.
 ):%0A     
@@ -424,16 +424,104 @@
 init__()
+%0A        self.polltimeout = polltimeout%0A        self.commit_frequency = commit_frequency
 %0A%0A      
@@ -916,19 +916,8 @@
 _uri
-, echo=True
 )%0A  
@@ -998,24 +998,49 @@
 ll(engine)%0A%0A
+        t0 = time.time()%0A
         whil
@@ -1090,19 +1090,32 @@
 er.poll(
-100
+self.polltimeout
 ))%0A     
@@ -1380,22 +1380,116 @@
     
-session.commit
+if time.time() - t0 %3E self.commit_frequency:%0A                session.commit()%0A                t0 = time.time
 ()%0A 

Add a commit-frequency parameter to the writer