【问题标题】:Passing session to backgroundTask cherrypy将会话传递给 backgroundTaskcherrypy
【发布时间】:2017-10-26 22:02:38
【问题描述】:

我在为后台任务正确配置函数以接收cherrypy 时遇到问题。我希望后台任务使用会话数据,能够重新生成当前会话并让会话过期。

这是我正在尝试的一个例子

import cherrypy
import cherrypy.process.plugins

class MainApp(object):

    def signin(self,user,pass):
        cherrypy.session['username'] = username
        cherrypy.session['password']  = password

    def communicateWithServer(self):
        user = cherrypy.session.get('username')
        password = cherrypy.session.get('password')
        response = requests.get("http://someserver/api?username="+user+"&password="+password)

    cherrypy.process.plugins.BackgroundTask(600, communicateWithServer (object)).start()

然后我得到这个错误

  Traceback (most recent call last):
  File "main.py", line 34, in <module>
    class MainApp(object):
  File "main.py", line 202, in MainApp
    cherrypy.process.plugins.BackgroundTask(600, communicateWithServer(object)).start()
  File "main.py", line 191, in loginReport
    user = cherrypy.session.get('username')
AttributeError: 'module' object has no attribute 'session'

将会话传递给 backgroundTask 的正确方法是什么?

【问题讨论】:

  • cherrypy.session 是线程本地的,它绑定到用户的请求,因此仅在 HTTP 请求-响应流期间可用。您能否具体说明您要完成的高级任务?
  • 我想使用会话存储用户名和密码来重复联系登录服务器,以确保用户显示为在线其他用户。我可以将数据保存到文件中,但认为会话会更优雅。
  • 您不能以这种方式使用会话。您可能想为此使用 Redis 商店之类的东西。
  • P.S.请随时在gitter 上提问,也许其他人可能会提出比这更好的建议:)

标签: python web-applications cherrypy


【解决方案1】:

由于会话不能按照我最初的意图使用,因此我使用 sqlite3 数据库代替存储会话中的数据,然后从那里与服务器通信。

【讨论】:

    猜你喜欢
    • 2020-07-19
    • 2011-07-24
    • 2012-06-02
    • 2023-04-03
    • 2018-07-11
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2018-02-12
    相关资源
    最近更新 更多