gpt4 book ai didi

python - 在 Flask 中使用 Redis 跟踪在线用户

转载 作者:可可西里 更新时间:2023-11-01 11:21:21 24 4
gpt4 key购买 nike

我正在尝试使用带有 flask 的 Redis 来跟踪我所有的在线用户。我使用 Redis 而不是全局字典的原因是我可以为 session 设置超时。

否则,如果用户登录超过 10 分钟,我将不得不运行一个线程来定期删除用户。

我按照此处的代码将 Redis 包含在我的 flask 项目中 http://flask.pocoo.org/snippets/71/我在 View 中创建了 redis 对象,并在 View 中包含了辅助函数。但是,当我在 get_online_users() 中调用 p.sunion(...) 函数时,我得到了这个堆栈跟踪

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.

最佳答案

确保你已经安装了 redis,它没有内置到任何发行版中。Redis 团队建议从下载安装稳定版本(即不通过您的 linux 发行版存储库)。这是我所做的:

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

通过发出 redis-cli 命令确保服务器正在运行,您应该会看到提示(默认提示为“127.0.0.1:6379>”)。如果您遇到该提示时遇到任何问题,请在 SO 上发布有关该问题的具体问题。

关于python - 在 Flask 中使用 Redis 跟踪在线用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30017398/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com