Flask-RQ2 icon indicating copy to clipboard operation
Flask-RQ2 copied to clipboard

Add getting started section to docs

Open jezdez opened this issue 6 years ago • 7 comments

This could use the code or part of it in https://github.com/jezdez/rq2test.

jezdez avatar Mar 02 '18 19:03 jezdez

The current README is a good start, it's just missing a simple example. Maybe adding a section like this would be sufficient?


Example Usage

Define a job and queue it:

# app.py

from flask import Flask
from flask_rq2 import RQ

app = Flask(__name__)
app.config['RQ_REDIS_URL'] = 'redis://127.0.0.1:6379/0'

rq = RQ(app)


@rq.job 
def foo():
    some_long_running_task()


@app.route('/')
def index():
    foo.queue()
    return 'Job queued!'

Start a worker process:

$ export FLASK_APP=app.py # Required for jobs to run within the Flask app context
$ flask rq worker

And you're ready to go!

timorthi avatar Mar 02 '18 21:03 timorthi

@timorthi I got AttributeError: module '__main__' has no attribute 'add' Any thoughts? Many thanks!

easyandme avatar Mar 10 '18 00:03 easyandme

Hey @maxim-xu, if you're following the example I posted above, there's no add() function defined anywhere, only foo(). Other than that I don't think I can help you without looking at your code.

timorthi avatar Mar 10 '18 00:03 timorthi

@timorthi Sorry! I'd appreciate it if you could take a look at this. I just replaced foo() with add. The rest of the code is almost the same, except that at the bottom I have:

if __name__ == "__main__": 
    port = int(os.environ.get("PORT", 5000))
    application.run(port=port, debug=False)

because I was running the flask server locally. When I run: python app.py & flask rq worker it shows the error I mentioned above.

easyandme avatar Mar 10 '18 01:03 easyandme

@maxim-xu Ah ok, this seems to be happening because you're using the Flask.run() command instead of the CLI. In your case the app's __name__ is __main__, which is why the worker process is trying to resolve __main__.foo(). I think you could get around this by naming your file __main__.py, but that's probably not recommended.

I'm not too well-versed with how Flask CLI or RQ handles __name__ with regard to the app instance so I can't comment further, but if you start your app using the Flask CLI FLASK_APP=app.py flask run then it'll solve your problem. Initializing your app instance with something like application = Flask('app') (assuming your file name is app.py) should do the trick too.

timorthi avatar Mar 10 '18 02:03 timorthi

@timorthi Solved. Thank you so much!

easyandme avatar Mar 10 '18 20:03 easyandme

My app runs fine, but when I do a 'flask rq worker' on the CLI to start the worker process I'm getting:

"Error: Detected factory 'create_app' in module 'app', but could not call it without arguments. Use 'app:create_app(args)' to specify arguments."

I'm using an application factory pattern with blueprints.

Any thoughts?

blacksteel1288 avatar Aug 28 '23 00:08 blacksteel1288