两个模块是否应该使用相同的 redis 连接?(我正在使用 Flask) [英] Should two modules use the same redis connection? (I'm working with Flask)
本文介绍了两个模块是否应该使用相同的 redis 连接?(我正在使用 Flask)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个使用 Redis 队列的 Flask 应用程序.工人的代码是:
I'm building a Flask app that uses a Redis Queue. The code for the worker is:
listen = ['default']
#redis_url = os.getenv('REDISTOGO_URL', 'redis://localhost:6379')
conn = redis.from_url(redis_url)
if __name__ == '__main__':
with Connection(conn):
worker = Worker(list(map(Queue, listen)))
worker.work()
另一个模块 app.py 包含处理 Flask 路由的代码.我的问题是,app.py 是否应该创建一个新的 Redis 连接:
And another module, app.py contains the code to handle Flask routes. My question is, should app.py create a new Redis connection as:
q = Queue(connection= redis.from_url(redis_url))
q.enqueue_call(func=mailers.send_message, kwargs=request.json, result_ttl=86400)
还是应该用app.py
Or should app.py use
import conn from worker
使用那个连接?
推荐答案
我会说使用新的连接,除非你真的有充分的理由不这样做(虽然我无法想象这样的理由)
I'd say use a new connection unless you really have a good reason not to (although I can't imagine such reason)
这篇关于两个模块是否应该使用相同的 redis 连接?(我正在使用 Flask)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文