gpt4 book ai didi

Python Tornado 更新请求之间的共享数据

转载 作者:太空狗 更新时间:2023-10-29 21:39:54 25 4
gpt4 key购买 nike

我有一个 Python Tornado 应用程序。该应用程序包含请求处理程序,为此我正在将数据传递给 like(下面的代码不完整,只是为了说明我想要什么):

configs = {'some_data': 1, # etc.
}

class Application(tornado.web.Application):
def __init__(self):
handlers = [('/pageone', PageOneHandler, configs),
('/pagetwo', PageTwoHandler, configs)]
settings = dict(template_path='/templates',
static_path='/static', debug=False)
tornado.web.Application.__init__(self, handlers, **settings)

# Run the instance
# ... code goes here ...
application = Application()
http_server = tornado.httpserver.HTTPServer(application)
# ... other code (bind to port, etc.)

# Callback function to update configs
some_time_period = 1000 # Once an second
tornado.ioloop.PeriodicCallback(update_configs, some_time_period).start()
tornado.ioloop.IOLoop.instance().start()

我希望 update_configs 函数更新上面定义的 configs 变量,并让此更改通过处理程序传播。例如(我知道这行不通):

def update_configs():
configs['some_data'] += 1

# Now, assuming PageOneHandler just prints out configs['some_data'], I'd expect
# the output to be: "1" on the first load, "2" if I load the page a second
# later, "4" if I load the page two seconds after that, etc.

问题是,configs 变量在 Application 类的构造函数中创建期间被传递给处理程序。如何在定期回调函数中更新 configs['some_data']

我对这种机制的实际用例是每隔一段时间从数据库中刷新存储在 configs 字典中的数据。

有没有一种简单的方法可以做到这一点而无需摆弄 application.handlers(我在过去一个小时左右尝试过)?

最佳答案

嗯,最简单的方法是将整个配置字典传递给处理程序,而不仅仅是字典中的各个值。因为字典是可变的,所以您对字典中的值所做的任何更改都会传播到所有处理程序:

import tornado.web
import tornado.httpserver

configs = {'some_data': 1, # etc.
}

def update_configs():
print("updating")
configs['some_data'] += 1

class PageOneHandler(tornado.web.RequestHandler):
def initialize(self, configs):
self.configs = configs
def get(self):
self.write(str(self.configs) + "\n")


class PageTwoHandler(tornado.web.RequestHandler):
def initialize(self, configs):
self.configs = configs

def get(self):
self.write(str(self.configs) + "\n")


class Application(tornado.web.Application):
def __init__(self):
handlers = [('/pageone', PageOneHandler, {'configs' : configs}),
('/pagetwo', PageTwoHandler, {'configs': configs})]
settings = dict(template_path='/templates',
static_path='/static', debug=False)
tornado.web.Application.__init__(self, handlers, **settings)

# Run the instance
application = Application()
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8888)

# Callback function to update configs
some_time_period = 1000 # Once an second
tornado.ioloop.PeriodicCallback(update_configs, some_time_period).start()
tornado.ioloop.IOLoop.instance().start()

输出:

dan@dantop:~> curl localhost:8888/pageone
{'some_data': 2}
dan@dantop:~> curl localhost:8888/pageone
{'some_data': 3}
dan@dantop:~> curl localhost:8888/pagetwo
{'some_data': 4}
dan@dantop:~> curl localhost:8888/pageone
{'some_data': 4}

对我来说,这种方法最有意义; configs 中包含的数据实际上并不属于 RequestHandler 的任何一个实例,它是所有 RequsetHandler 以及您的 PeriodicCallback 。所以我认为尝试创建该状态的 X 个副本,然后尝试手动保持所有这些不同的副本同步是没有意义的。相反,只需使用带有类变量的自定义对象或字典在整个流程中共享状态,如上所示。

关于Python Tornado 更新请求之间的共享数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25067916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com