sahibeast
sahibeast

Reputation: 351

Using Redis to keep track of online users in Flask

I am trying to user Redis with flask to keep track of all of my users that are online. The reason I am using Redis instead of a global dictionary, is so that I can set a timeout for the sessions.

Otherwise I would have to run a thread to delete users periodically if they have been logged in for more than 10 minutes.

I followed the code here to include Redis in my flask project http://flask.pocoo.org/snippets/71/ I made the redis object in views and included the helper functions in views as well. However I am getting this stack trace when I call the p.sunion(...) function in get_online_users()

return self.wsgi_app(environ, start_response)
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site-             
packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site-  
packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site- 
packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site- 
packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site- 
packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site- 
packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site
packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/sahilshah/Documents/2015 Spring Classes/CIS  
350/studybuddy/sahil/app/views.py", line 23, in signin
online_users = get_online_users()
File "/Users/sahilshah/Documents/2015 Spring Classes/CIS  
350/studybuddy/sahil/app/views.py", line 102, in get_online_users
for x in minutes])
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site- 
packages/redis/client.py", line 1543, in sunion
return self.execute_command('SUNION', *args)
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site-   
packages/redis/client.py", line 570, in execute_command
connection.send_command(*args)
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site- 
packages/redis/connection.py", line 556, in send_command
self.send_packed_command(self.pack_command(*args))
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site-  
packages/redis/connection.py", line 532, in send_packed_command
self.connect()
File "/Users/sahilshah/.virtualenvs/venv/lib/python2.7/site
packages/redis/connection.py", line 436, in connect
raise ConnectionError(self._error_message(e))
ConnectionError: Error 61 connecting to localhost:6379. Connection refused.

Upvotes: 0

Views: 1052

Answers (1)

GG_Python
GG_Python

Reputation: 3541

Make sure you have redis installed, it's not built-into any distribution. The Redis team recommends installing stable version from download (namely not through your linux distribution repo). Here's what I did:

   wget http://download.redis.io/redis-stable.tar.gz
   tar xvzf redis-stable.tar.gz
   cd redis-stable
   make hiredis jemalloc linenoise lua

Make sure you have a server running by issuing a redis-cli command, you should see a prompt (default prompt is "127.0.0.1:6379>"). If you have any problems to get to that prompt, post specific question regarding that problem on SO.

Upvotes: 1

Related Questions