Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I use global variable to fit my need:

scores = {}

def update():
    global scores
    # fetch data from database
    scores = xxx

class MyRequestHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        def choice_with_score(items):
            # return a key related to scores
            return key

        def get_key():
            global scores
            return choice_with_score(scores)

        self.write('%s' % get_key())
        self.finish()

if __name__ == '__main__':
    # initially update
    update()

    app = tornado.web.Application([
        (r'.*', MyRequestHandler),
    ])

    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(port)
    ioloop = tornado.ioloop.IOLoop.instance()

    # background update every x seconds
    task = tornado.ioloop.PeriodicCallback(
            update,
            15 * 1000)
    task.start()

    ioloop.start()

Here,function update() to get new data instead of updating in every incoming request,so I use global variable.

Is there other ways to do the same work?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.