Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getting started section to docs #52

Open
jezdez opened this issue Mar 2, 2018 · 7 comments
Open

Add getting started section to docs #52

jezdez opened this issue Mar 2, 2018 · 7 comments

Comments

@jezdez
Copy link
Member

jezdez commented Mar 2, 2018

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

@jezdez jezdez changed the title Add getting started section Add getting started section to docs Mar 2, 2018
@timorthi
Copy link

timorthi commented Mar 2, 2018

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!

@easyandme
Copy link

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

@timorthi
Copy link

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.

@easyandme
Copy link

@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.

@timorthi
Copy link

timorthi commented Mar 10, 2018

@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.

@easyandme
Copy link

@timorthi Solved. Thank you so much!

@blacksteel1288
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants