将会话传递给backgroundTask cherrypy

问题描述:

我无法正确配置我的后台任务的功能,以获取cherrypy。我希望后台任务使用会话数据,能够重新生成当前会话并使会话过期。将会话传递给backgroundTask 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的正确方法的例子吗?

+1

'cherrypy.session'是一个线程本地的,它绑定到用户的请求,因此只在HTTP请求 - 响应流程中可用。 您能否指定您尝试实现哪个高级任务? – webKnjaZ

+0

我想使用会话来存储用户名和密码,以反复联系登录服务器以确保用户显示为对其他用户在线。我可以将这个保存数据保存到一个文件中,但是认为会话会更加合适。 – AmatuerCoder101

+1

你不能以这种方式使用会话。您可能希望像这样的Redis商店使用smth。 – webKnjaZ

由于会话无法使用,因为我原本打算使用sqlite3数据库,而不是存储会话中的数据,然后从那里与服务器进行通信。