Search code examples
pythonweb-applicationscherrypy

Passing session to backgroundTask cherrypy


I am having trouble correctly configuring my function for my background task to take in cherrypy. I want the background task to use session data, be able to regenerate the current session and also expire the session.

this is an example of what im trying

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()

but then I get this error

  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'

What is the correct method of passing sessions to a backgroundTask?


Solution

  • Since sessions can not be used as I originally intending I am using a sqlite3 data base instead to store the data that would have been in the session and then communicating with the server from there.