uWSGI + Flask + boto - 线程安全 [英] uWSGI+Flask+boto - thread safety
问题描述
uwsgi --socket 127.0.0.1 :3031 - file flaskapp.py --callable app --processes 4
我的Flask应用程序是像这样组织:
/ flaskapp
app.py
/ db
__init__.py
somefile.py
somefile2.py
...
我正在使用boto连接到DynamoDB。 __ init __。py
文件是空的,并且每个 somefilexxx.py
文件都是这样开始的:
$ table $ db $ b $ do_stuff_with_table():
我不使用应用程序中的线程,我不认为uWSGI使用线程,除非我明确用 - threads
来启用它们。这个设置是否有意义?有没有任何线程问题,我不得不担心urllib(你可能猜测我知道线程没有什么比... ...)?
另外,它会更有意义在 __ init __。py
文件中调用 connect_dynamodb()
,只加载 somefile.py
files?
由于您尚未在uWSGI中启用线程(请参阅 - 启用线程
, - 线程
)这里没有Python线程正在进行(以Boto或其他方式) / p>
我会推荐使用 - lazy
,这会导致您的应用程序被加载到每个worker post-fork中。那么你可以简单地依靠这种行为,以确保每个工人有适当的连接/池/等。没有共享状态的担忧。
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?
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.
这篇关于uWSGI + Flask + boto - 线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!