uWSGI+Flask+boto - thread safety

≯℡__Kan透↙ 提交于 2019-12-10 20:09:10

问题


Say I have a Flask application, served by uWSGI using multiple processes, like:

uwsgi --socket 127.0.0.1:3031 --file flaskapp.py --callable app --processes 4

And my Flask app is organized like this:

/flaskapp
    app.py
    /db
        __init__.py
        somefile.py
        somefile2.py
        ...

And I'm using boto to connect to DynamoDB. The __init__.py file is empty, and each somefilexxx.py file begins something like this:

db = boto.connect_dynamodb()
table = db.get_table('table')
def do_stuff_with_table():

I don't use threads in the app, and I don't think uWSGI uses threads unless I explicitly enable them with --threads. Does this setup make sense? Are there any threading issues I have to worry about with urllib (you might guess I know less than nothing about threads...)?

Alternatively, would it make more sense to call connect_dynamodb() in the __init__.py file and only load the tables in the somefile.py files?


回答1:


Since you haven't enabled threads in uWSGI (see: --enable-threads, --threads) there's no Python threading going on here (in Boto or otherwise.)

I would recommend using --lazy, which will cause your app to be loaded in each worker post-fork. Then you can simply rely on that behavior to ensure each worker has the appropriate connections/pools/etc. available without the concerns of shared state.



来源:https://stackoverflow.com/questions/11640031/uwsgiflaskboto-thread-safety

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!