Should two modules use the same redis connection?

2019-07-20 17:15发布

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()

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) 

Or should app.py use

import conn from worker

And use that connection?

1条回答
够拽才男人
2楼-- · 2019-07-20 18:01

I'd say use a new connection unless you really have a good reason not to (although I can't imagine such reason)

查看更多
登录 后发表回答