简体   繁体   English

如何在Tornado Web中的请求之间共享数据

[英]How to share data between requests in Tornado Web

I have the following use case for my Tornado web server: 我的Tornado Web服务器有以下用例:

Upon POST requests entries can be made to the server, entries which will not be persisted to a file or database. 在POST请求时,可以向服务器输入条目,这些条目不会持久保存到文件或数据库。 Upon GET requests a process can be started or terminated. 在GET请求时,可以启动或终止进程。

Hence I need to share data between different requests in my RequestHandler implementation. 因此,我需要在RequestHandler实现中的不同请求之间共享数据。 What is the normal way to do so? 这样做的正常方法是什么?

I had difficulties saving data to self , for instance self.entry = "..." . 我在将数据保存到self遇到了困难,例如self.entry = "..." In another request the data was not present anymore. 在另一个请求中,数据不再存在。

The only working solution I've found is to store that in the application object: 我发现唯一可行的解​​决方案是将其存储在应用程序对象中:

    application = web.Application([
            (r'.*', MainHandler,
            ])

and

    def get(self):
         # ...
         self.application.entry = "..."

Is that the proper way? 这是正确的方法吗? Also what about synchronization here, I mean this means access to shared data. 那么同步在这里,我的意思是这意味着访问共享数据。

I suggest the following: Instead of a database access object pass an object which stores your data, for instance: 我建议如下:代替数据库访问对象传递存储数据的对象,例如:

data = DataStore()

application = web.Application([
        (r'.*', MainHandler, dict(data = data),
        ])

with the following RequestHandler initialization method. 使用以下RequestHandler初始化方法。

def initialize(self, data):
     self.data = data

You have to create the object before and pass it, otherwise it will be recreated every time a request is processed. 您必须先创建对象并传递它,否则每次处理请求时都会重新创建它。

The documentation gives a way to do this : 文档提供了一种方法:

class MyHandler(RequestHandler):
    def initialize(self, database):
        self.database = database

    def get(self, username):
        ...

mydatabase = dict()

app = Application([
    (r'/user/(.*)', MyHandler, dict(database=mydatabase)),
    ])

Then you can save your object mydatabase to a file. 然后,您可以将对象mydatabase保存到文件中。

But I'm not sure this is the right way to achieve what you want regarding synchronization between requests. 但我不确定这是实现请求之间同步所需的正确方法。

You can use memcached for something like this. 你可以使用memcached的东西。 However, you'll need to setup the memcached server. 但是,您需要设置memcached服务器。

http://pypi.python.org/pypi/python-memcached/ http://pypi.python.org/pypi/python-memcached/

Application is the right object to store (semi-)persistent data. 应用程序是存储(半)持久数据的正确对象。 However, as suggested on other anwser, you should consider using some kind of database to store this data. 但是,正如其他anwser所建议的那样,您应该考虑使用某种数据库来存储此数据。

However, you should take care that wen a session (or transaction) doesn't finish properly (eg you get a POST but no GET to trigger the action), you should delete the session data so as not to have your webserver leak memory. 但是,您应该注意会话(或事务)没有正确完成(例如,您获得POST但没有GET来触发操作),您应该删除会话数据,以免让您的Web服务器泄漏内存。

From my experience, I'd suggest using Redis since it is easy to use and supports key expiration , a mechanism that comes handy when you need to manage session data. 根据我的经验,我建议使用Redis,因为它易于使用并支持密钥过期 ,这种机制在您需要管理会话数据时非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM