简体   繁体   English

在Flask工作人员之间共享可变数据

[英]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: 为解决此问题,我提出了两种一般方法:

  1. Somehow have a shared local copy of the values that can be modified by each worker. 以某种方式拥有每个工作人员都可以修改的值的共享本地副本。 Then when the user changes the value, the worker will update both the database and the local variable. 然后,当用户更改值时,工作程序将同时更新数据库和本地变量。 shelve may be a good option for this approach. shelve可能是此方法的一个不错的选择。
  2. Somehow notify each worker that the values need to be reloaded from the database. 以某种方式通知每个工作人员需要从数据库中重新加载值。

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对象(甚至使用picklectypes对象)。 See the documentation for multiprocessing here , and specifically, the sharedctypes submodule . 请参阅此处multiprocessing文档,尤其是sharedctypes子模块

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 gunicorn 工人之间共享内存中的动态数据 - Share dynamic data in memory between gunicorn workers Python Ray:如何在工作人员之间共享变量? - Python Ray: How to share variable between workers? Ray究竟是如何将数据共享给worker的呢? - How exactly does Ray share data to workers? 如何在 Gunicorn 工作人员之间共享缓存? - How can I share a cache between Gunicorn workers? 进程(芹菜工人)之间是否可以共享子进程管道? - is it possible to share subprocess pipe between processes (celery workers)? 如何使用 FastAPI + uvicorn 在工作人员之间共享数据库连接? - How to share database connection between workers using FastAPI + uvicorn? 在一台机器上共享芹菜工人之间的记忆区域 - Share memory areas between celery workers on one machine Flask-SocketIo,如何在python线程和socketio.start_background_task线程之间共享数据? - Flask-SocketIo, How to share data between python thread and socketio.start_background_task thread? Flask 中的全局变量是线程安全的吗? 如何在请求之间共享数据? - Are global variables thread-safe in Flask? How do I share data between requests? 在 Flask 和其他应用程序之间共享 sqlalchemy 模型 - Share sqlalchemy models between flask and other apps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM