[Flask] Separate initialization for each worker in Flask+Gunicorn
Alejo Arias
alejoar at gmail.com
Mon Jan 18 08:33:35 EST 2016
Hi Abhijeet,
You can set it at any time (after app creation). To do so, import
current_app from flask:
from flask import current_app
And then access the config parameter like so:
To set:
current_app.config['CURRENT_TOKEN'] = 'your_token_here'
To get:
my_token = current_app.config['CURRENT_TOKEN']
Hope that helps.
Regards,
Alejo
On 18 January 2016 at 12:39, Abhijeet Rastogi <abhijeet.1989 at gmail.com>
wrote:
> Hi Alejo,
>
> Thanks for the reply. How should I set this value? After I initialize my
> "app" variable and just before I register blueprints?
>
> Cheers!
>
> On Mon, Jan 18, 2016 at 5:02 PM Alejo Arias <alejoar at gmail.com> wrote:
>
>> Hi Abhijeet,
>>
>> You could store the token as an app config value:
>>
>> app.config['CURRENT_TOKEN'] = 'your_token_here'
>>
>> and then have the workers access the token from the app config. Anytime a
>> worker finds the token is not set or needs an update, simply set/update the
>> same config value.
>>
>> Regards,
>> Alejo
>>
>> On 18 January 2016 at 08:31, Abhijeet Rastogi <abhijeet.1989 at gmail.com>
>> wrote:
>>
>>> Hi All,
>>>
>>>
>>> My flask application views contact an API endpoint during the lifetime
>>> of requests. Each of these API request requires a token. Due to the server
>>> side limitation, I can't have two threads using the same token for
>>> initiating requests.
>>>
>>> Requirements:-
>>>
>>> - To contact this API, I need to fetch an API token. I would like to
>>> do this once per worker and then share that value among views. (just a
>>> read-only variable).
>>> - I want each "worker" that serves requests to have a separate
>>> "token". How can that be achieved in Flask?
>>> - I don't want to issue a fresh token each time a request is served
>>> as that'll be horribly slow.
>>>
>>> How do I make that possible in Flask? Any help is appreciated.
>>>
>>> Thanks!
>>>
>>
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160118/aa2ef1de/attachment-0001.html>
More information about the Flask
mailing list