librelist archives

« back to archive

Using application context in rq worker

Using application context in rq worker

From:
Michael Graupner
Date:
2015-03-10 @ 16:55
I'm having problems using the application context of the flask app in a rq
worker (http://python-rq.org/).

My application structure is the one used by Miguel Grinbergs flasky app:
https://github.com/miguelgrinberg/flasky

The only place where the app object exists is the manage.py and I don't
know how to access this from  the workers perspective or how to initialize
a worker with the app context. Could someone give me a little hint or show
me some examle code how to do this properly? The goald should be the
creaetion of database entries with the database connection from the app
context, accessing the url router and using the template engine in the
worker.

Thanks for your help.

Re: [flask] Using application context in rq worker

From:
Jonathan Chen
Date:
2015-03-10 @ 17:34
You can take a look at the project called flask-rq for some more help. In
general though you'll need to pass the context of the object with your jobs
in order for it to work correctly. You'll still need to do something
similar in the following:

@job
def random():
    with app.app_context():
        x = 1 + 1



Thanks,

Jonathan C.
Vice President of IT | Sunrider International
http://www.sunrider.com



On Tue, Mar 10, 2015 at 9:55 AM, Michael Graupner <mgraupner@gmail.com>
wrote:

> I'm having problems using the application context of the flask app in a rq
> worker (http://python-rq.org/).
>
> My application structure is the one used by Miguel Grinbergs flasky app:
> https://github.com/miguelgrinberg/flasky
>
> The only place where the app object exists is the manage.py and I don't
> know how to access this from  the workers perspective or how to initialize
> a worker with the app context. Could someone give me a little hint or show
> me some examle code how to do this properly? The goald should be the
> creaetion of database entries with the database connection from the app
> context, accessing the url router and using the template engine in the
> worker.
>
> Thanks for your help.
>

CONFIDENTIALITY NOTICE:  This message is intended only for the use of the 
individual or entity to which it is addressed, and may contain information
that is privileged, confidential, and exempt from disclosure under 
applicable law.  If you have received this email in error, please 
immediately notify the sender by return email and delete this email and 
any attachments from your system.

Re: [flask] Using application context in rq worker

From:
Michael Graupner
Date:
2015-03-11 @ 08:05
Thanks Jonathan,

I already knew about the with app.app_context(): part but could not figure
out how to get the context into the worker... After your answer I looked at
how flask-rq is doing it (I did not want to use it because it was not
updated since 2 years and I try to avoid old unmaintained add-ons) and
finally found a clue in this stackoverflow question:
http://stackoverflow.com/questions/19031359/flask-context-and-python-rq

After getting the imports of the app right (from manage import app) and
setting the worker path of rqworker to my applications directory everything
works as expected.

Thanks,
Michael

2015-03-10 18:34 GMT+01:00 Jonathan Chen <jonathan.chen@sunrider.com>:

> You can take a look at the project called flask-rq for some more help. In
> general though you'll need to pass the context of the object with your jobs
> in order for it to work correctly. You'll still need to do something
> similar in the following:
>
> @job
> def random():
>     with app.app_context():
>         x = 1 + 1
>
>
>
> Thanks,
>
> Jonathan C.
> Vice President of IT | Sunrider International
> http://www.sunrider.com
>
>
>
> On Tue, Mar 10, 2015 at 9:55 AM, Michael Graupner <mgraupner@gmail.com>
> wrote:
>
>> I'm having problems using the application context of the flask app in a
>> rq worker (http://python-rq.org/).
>>
>> My application structure is the one used by Miguel Grinbergs flasky app:
>> https://github.com/miguelgrinberg/flasky
>>
>> The only place where the app object exists is the manage.py and I don't
>> know how to access this from  the workers perspective or how to initialize
>> a worker with the app context. Could someone give me a little hint or show
>> me some examle code how to do this properly? The goald should be the
>> creaetion of database entries with the database connection from the app
>> context, accessing the url router and using the template engine in the
>> worker.
>>
>> Thanks for your help.
>>
>
> CONFIDENTIALITY NOTICE:  This message is intended only for the use of 
the individual or entity to which it is addressed, and may contain 
information that is privileged, confidential, and exempt from disclosure 
under applicable law.  If you have received this email in error, please 
immediately notify the sender by return email and delete this email and 
any attachments from your system.
>
>