[英]Share mutable data between Flask workers
I have a Flask app with some user-modifiable config values that are stored in a database. 我有一个Flask应用程序,其中包含一些用户可修改的配置值,这些配置值存储在数据库中。 The values won't change often, so I don't want to fetch them from the database each time I get a request (the values are needed for every request), but I also need changes to propagate immediately to all workers.
这些值不会经常更改,因此我不想每次收到请求时都从数据库中获取它们(每个请求都需要这些值),但是我还需要更改才能立即传播给所有工作人员。
The two general approaches I've come up with for solving this issue are: 为解决此问题,我提出了两种一般方法:
shelve
may be a good option for this approach. shelve
可能是此方法的一个不错的选择。 What would be the best way to do this? 最好的方法是什么?
(Note: the app is served by Gunicorn on a Heroku Hobby Dyno.) (注意:该应用程序由Gunicorn在Heroku Hobby Dyno上提供。)
In the end I learned about Python's multiprocessing
module, which provides, among other things, a fairly simple way to share memory between processes. 最后,我了解了Python的
multiprocessing
模块,该模块除其他外提供了一种非常简单的方法来在进程之间共享内存。
In my case I needed to share a string, so I initialized a shared string with 就我而言,我需要共享一个字符串,因此我使用
import multiprocessing as mp
...
shared_str = mp.Array('c', 100)
This creates a wrapper for a shared c-string of length 100. The multiprocessing.Array
constructor takes a type argument (either a ctypes
class or a type-code string and a length. I can then access the shared string with shared_str.value
. 这将为长度为100的共享c字符串创建一个包装器
multiprocessing.Array
构造函数接受一个类型参数( ctypes
类或类型代码字符串和一个长度。然后,我可以使用shared_str.value
访问共享字符串。
I run gunicorn
with the --preload
option, so the shared memory is allocated when my app is created, and then each process can access it with its own copy of shared_str
. 我使用
--preload
选项运行gunicorn
,因此在创建我的应用程序时会分配共享内存,然后每个进程都可以使用其自己的shared_str
副本访问它。
This method will work for other ctypes
objects as well (and even non- ctypes
objects, using pickle
). 此方法也适用于其他
ctypes
对象(甚至使用pickle
非ctypes
对象)。 See the documentation for multiprocessing
here , and specifically, the sharedctypes
submodule . 请参阅此处的
multiprocessing
文档,尤其是sharedctypes
子模块 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.