Skip to content

Add getting started section to docs #52

Open

Description

@jezdez

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

Activity

  1. changed the title [-]Add getting started section[/-] on Mar 2, 2018
  2. timorthi commented on Mar 2, 2018

    @timorthi

    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!

  3. easyandme commented on Mar 10, 2018

    @easyandme

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

  4. timorthi commented on Mar 10, 2018

    @timorthi

    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.

  5. easyandme commented on Mar 10, 2018

    @easyandme

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

  6. timorthi commented on Mar 10, 2018

    @timorthi

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

  7. easyandme commented on Mar 10, 2018

    @easyandme

    @timorthi Solved. Thank you so much!

  8. blacksteel1288 commented on Aug 28, 2023

    @blacksteel1288

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions