问题
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