1

我刚刚开始使用flask,正在尝试使用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以查看代码运行时发生的情况,这是打印到终端的内容。

* 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>>>
4

1 回答 1

0

我相信我已经解决了这个问题。问题是我打开 apipeline并尝试在调用之前对从数据库检索到的数据进行操作pipe.execute。我仍在努力使整体功能正常工作,但这就是我解决这个特定问题的方法。

于 2014-03-10T18:03:50.107 回答