1

enter image description here

I'm trying to extend the flask-base project https://github.com/hack4impact/flask-base/tree/master/app which comes with a user model only. I'm trying to add the ability to run a background task on redis using rq. I've found https://devcenter.heroku.com/articles/python-rq which is helpful.

this app has support for redis queues with a background redis queue being implemented by running :

@manager.command
def run_worker():
    """Initializes a slim rq task queue."""
    listen = ['default']
    conn = Redis(
        host=app.config['RQ_DEFAULT_HOST'],
        port=app.config['RQ_DEFAULT_PORT'],
        db=0,
        password=app.config['RQ_DEFAULT_PASSWORD'])

    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

using:

$ python manage.py run_worker

In my views I have:

@main.route('/selected')
def background_selected():
    from rq import Queue
    from manage import run_worker.conn
    q = Queue(connection=conn)
    return q.enqueue(selected)

The problem is I don't know how to import the connection created in run_worker() into my view. I've tried variations of :

from manage import run_worker.conn

but I'm getting:

SyntaxError: invalid syntax.

How can I get access to the conn variable in the background task?

2 Answers 2

1

from the documentation, python-rq Configuration

Can you try by making the below changes:

manager.py

import redis

"""Initializes a slim rq task queue."""
listen = ['default']
conn = redis.Redis(host=app.config['RQ_DEFAULT_HOST'], 
                   port=app.config['RQ_DEFAULT_PORT'],
                   db=0,     
                   password=app.config['RQ_DEFAULT_PASSWORD'])

@manager.command
def run_worker():
    with Connection(conn):
        worker = Worker(map(Queue, listen))
        worker.work()

and from view:

from rq import Queue
from manage import conn

q = Queue(connection=conn)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. I didn't realize it before but there is a flask addon called flask-rq (pythonhosted.org/Flask-RQ) which provides a @job decorator to allow background tasks to be run.
1

I contacted the developer who provided the following:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.