我刚开始使用刻录机,我正在尝试使用redis构建教程微博。这是我的应用程序:
from flask import Flask, render_template, request, url_for, redirect
import redis
from datetime import datetime
app = Flask(__name__)
app.config.from_object(__name__)
app.config.update(dict(
DEBUG = True,
))
POOL = redis.ConnectionPool(host='localhost', port=6379, db=0)
@app.route("/")
def index():
r = redis.StrictRedis(connection_pool = POOL)
print r
pipe = r.pipeline()
print pipe
last_ten = pipe.zrange('post:created_on', 0, 9)
print last_ten
posts = []
for post in last_ten:
posts.append(pipe.hgetall('{}{}'.format('post:', post)))
print posts
pipe.execute()
return render_template('index.html', posts)
@app.route('/new', methods = ['POST'])
def addPost():
r = redis.StrictRedis(connection_pool = POOL)
title = request.form['title']
author = request.form['author']
now = datetime.now()
pipe = r.pipeline()
post_format = 'post:'
pid = pipe.incr('post')
pipe.hmset('{}{}'.format(post_format,pid), {'title':title, 'author':author})
pipe.zadd('post:created_on', pid = now)
pipe.execute()
return redirect(url_for('index'))
if __name__ == "__main__":
app.run()
当我运行python testapp.py
时,我得到了
* Running on http://127.0.0.1:5000/
* Restarting with reloader
但是页面永远不会在http://127.0.0.1:5000/
加载,也不会返回错误。它只是挂起,试图永远加载。我已经离开了一段时间,它一直在继续。我不确定会导致什么,但谢谢你的帮助。
更新:我在print
视图中添加了几个index
语句,以查看代码运行时发生的情况,这是打印到终端的内容。< / p>
* Running on http://127.0.0.1:5000/
* Restarting with reloader
StrictRedis<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
StrictPipeline<ConnectionPool<Connection<host=localhost,port=6379,db=0>>>
答案 0 :(得分:0)
我相信我已经解决了这个问题。问题是我打开了pipeline
并尝试在调用pipe.execute
之前对从db中检索到的数据进行操作。我仍在努力让整体功能发挥作用,但这就是我解决这一特定问题的方法。