Andrew Kloos
Andrew Kloos

Reputation: 4598

How do I determine the current number of CherryPy sessions?

I found code for an older version of CherryPy:

len(cherrypy.session.cache)

http://tools.cherrypy.org/wiki/ActiveSessionCount

but this no longer works in CherryPy 3.2.2.

Error => AttributeError: 'FileSession' object has no attribute 'cache'

Thanks in advance!

Upvotes: 1

Views: 245

Answers (1)

Andrew Kloos
Andrew Kloos

Reputation: 4598

Ok, since I'm storing session data in the file system I needed to go about getting the number of sessions a different way. Basically for each user a file is created in the session folder of my application based on my config settings...

tools.sessions.on = True
tools.sessions.storage_type = 'file'
tools.sessions.storage_path = 'sessions'

So I just need count the number of files in the sessions folder. I accomplish this by the following line of code:

NumberOfSessions=len([FileName for FileName in os.listdir(os.path.abspath(os.path.dirname('sessions')) + '/sessions') if not '.lock' in FileName and os.path.isfile(os.path.abspath(os.path.dirname('sessions')) + '/sessions/' + FileName)])

[this code excludes files with a *.lock extension or else a session could be counted twice]

Andrew

Upvotes: 1

Related Questions