How to create a global variable loaded every N minutes from a redis call

1,580 views
Skip to first unread message

David Montgomery

unread,
May 28, 2012, 9:52:42 AM5/28/12
to python-...@googlegroups.com
Hi,

Every e.g. 10 minutes, I would like to load a dictionary from a redis
call that is accessible to all other loop iterations for the next 10
minutes ...on and on.

class MyHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):

if min = 10:
myglobal = yield tornado.gen.Task(r.get,'global')

self.set_header('Content-Type', 'image/gif')
self.write(pixel)
sw.stop()
self.finish()


What is the best way to do this?

Thanks

Lorenzo Bolla

unread,
May 28, 2012, 10:21:44 AM5/28/12
to python-...@googlegroups.com
The way I usually do this is by having a PeriodicCallback loading and refreshing the same object (it does not have to be global) over and over.

Then, every handler is initialized with a reference to this object.

Something like:

===

class Application(tornado.web.Application):
    def __init__(self, store):
        self.store = store
        ...

class BaseHandler(tornado.web.RequestHandler):
    @property
    def store(self):
        # Inside a handler, refer to the store with "self.store"
        return self.application.store

def main():
    store = Store()
    # put in Store.load the call to Redis
    cb = tornado.ioloop.PeriodicCallback(store.load, 60000) # refresh the store

    application = Application(
        store=store,
        )   


hth,
L.

Chris Allick

unread,
May 28, 2012, 11:18:51 AM5/28/12
to python-...@googlegroups.com
Setting a dictionary at global level like

myglobal = {}

And then within a class you can write to it or read it

global myglobal
myglobal['data'] = 4

Phil Whelan

unread,
May 28, 2012, 12:47:13 PM5/28/12
to python-...@googlegroups.com
On Mon, May 28, 2012 at 7:21 AM, Lorenzo Bolla <lbo...@gmail.com> wrote:
PeriodicCallback

+1

Chris Allick

unread,
May 28, 2012, 1:54:28 PM5/28/12
to python-...@googlegroups.com
oh i see what you want. yeah periodiccallback for sure

def main():
    signal.signal(signal.SIGINT, signal_handler)

    c = Counter()
    c.start()

    tornado.options.parse_command_line()
    app = Application()
    app.listen(options.port)
    io_loop = tornado.ioloop.IOLoop.instance()
    tornado.ioloop.PeriodicCallback( ATWSocketHandler.update_coords, 250.0, io_loop=io_loop ).start()
    io_loop.start()

if __name__ == "__main__":
    main()

David Montgomery

unread,
May 29, 2012, 7:12:47 AM5/29/12
to python-...@googlegroups.com
Hi,

Thanks..but I kinda sorta get it. Yes...periodic call back is cool
but having an issue putting the pieces together. Below is my code. I
don.t get how to link the callback and how the results propagate to
the MyPageHandler. Below I am trying to get the callback to update
the time of the call.

class Application(tornado.web.Application):
def __init__(self,store):
self.store = store
print 'periodic callback', self.store
handlers = [
(r"/", MyPageHandler),
]
tornado.web.Application.__init__(self, handlers)

class BaseHandler(tornado.web.RequestHandler):
@property
def store(self):
# Inside a handler, refer to the store with "self.store"
return self.application.store

class MyPageHandler(BaseHandler):
def get(self):
self.set_header('Content-Type', 'text/plain')
self.write('test')
print 'test', self.store

def Store():
store = time.time()


if __name__ == '__main__':

store=5
application = Application(store=store,)

http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8049)
io_loop = tornado.ioloop.IOLoop.instance()
tornado.ioloop.PeriodicCallback(Store,3000,io_loop=io_loop).start()
io_loop.start()

Lorenzo Bolla

unread,
May 29, 2012, 8:47:19 AM5/29/12
to python-...@googlegroups.com
This is not the code I wrote in the first email.
If you take that code, the only thing you need to implement is a class Store, with one method "load".

class Store(object):
    def __init__(self):
        self.time_dependent_data = None
    def load(self):
        self.time_dependent_data = time.time()

Now, inside a handler you can access your time-dependent data:
self.store.time_dependent_data

PeriodicCallback will call "store.load" and update the "time_dependent_data" periodically.

L.

David Montgomery

unread,
May 29, 2012, 11:58:19 AM5/29/12
to python-...@googlegroups.com
Awesome! Thanks.worked like a charm:)
Reply all
Reply to author
Forward
0 new messages