在多个工作人员之间共享 python 个对象

Sharing python objects across multiple workers

我们已经使用 FastAPI 创建了一个服务。当我们的服务启动时,它会创建一些 python 对象,然后端点会使用这些对象来存储或检索数据。

生产中的 FastAPI 从多个 worker 开始。我们的问题是 每个工作人员都创建自己的对象,而不是共享一个对象

下面的脚本显示了我们正在做的事情的一个(简化)示例,尽管在我们的例子中,Meta() 的用法要复杂得多。

from fastapi import FastAPI, status

class Meta:
   def __init__(self):
      self.count = 0  

app = FastAPI()

meta = Meta()

# increases the count variable in the meta object by 1
@app.get("/increment")
async def increment():
    meta.count += 1
    return status.HTTP_200_OK

# returns a json containing the current count from the meta object
@app.get("/report")
async def report():
    return {'count':meta.count}

# resets the count in the meta object to 0
@app.get("/reset")
async def reset():
    meta.count = 0
    return status.HTTP_200_OK


如上所述,多个工人的问题是每个工人都有自己的 meta 对象。请注意,当 运行 与 api 有一个工人时,该问题不可见。

更明确地说,当我们第一次点击 /increment 端点时,我们将看到两个工作人员中只有一个响应呼叫(这是正确的,我们不希望两个工作人员都做同样的事情事物)。但是,因为有两个单独的 meta 对象,所以只有两个中的一个会递增。
当命中 /report 端点时,根据响应请求的工作人员,将返回 1 或 0。

那么问题来了,如何让worker共享并操作同一个对象呢?

作为附带问题,上述问题也会影响 /reset 端点。如果调用此端点,则只有一个工作人员将重置其对象。有没有办法强制所有工作人员响应端点上的单个呼叫?

谢谢!

编辑:我忘了提及我们曾尝试(但没有成功)将 meta 对象存储在 app.state 中。本质上:

app.state.meta = Meta()
...
@app.get("/report")
async def report():
    return {'count':app.state.meta.count}

问题 1

The question then is, how do we get the workers to share and operate on the same object?

TL;DR

虽然您可以通过 multiprocessing 之类的方式共享对象,但在您的用例中,您可能 最好使用缓存 ,例如 Redis。

说明

我根本不是 parallel/concurrent 应用程序方面的专家,但我知道除非你需要加速 非常昂贵的 CPU 绑定操作(即非常复杂 and/or 长时间运行的计算),您 不要 想要在进程之间共享对象。

您可以通过专用的库和模块来做到这一点,但是这会使您的应用程序 复杂得多 ,必须处理所有可能的竞争条件和并行性固有的边缘情况.如果你确实想走那条路,我相信有很多库和工具,但你应该首先看看 multiprocessing, the standard python library for handling parallelism. Check also this and this 关于使用它在 gunicorn 的工作人员之间共享资源。

另一方面,您的用例看起来不需要非常复杂的计算,因此我建议使用一个简单的缓存作为您的工作人员的“数据中心”,而不是 class。它将为您提供所需的结果,让您的流程拥有单一的真实来源,而无需共享内存的复杂性。

如果您想尝试这种方法,我建议您看一下 Redis,这是一种非常流行且得到良好支持的缓存解决方案,如果您愿意,它甚至可以保留数据。

这里有一个列表,Redis clients for python. redis-py是推荐的。


问题 2

As a side question, the problem above affects the /reset endpoint too. If this endpoint is called then only one of the workers will reset its object. Is there a way to force all workers to respond to a single call on an endpoint?

如果使用缓存,问题就会消失。你只有一个事实来源,你只需删除那里的数据,无论哪个工作人员响应请求。然后每个工作人员都会看到数据已被重置。

无法直接在不同进程之间共享 python 对象。 multiprocessing 模块中包含的设施(如 managersshared memory)不适合在 worker 之间共享资源,因为它们需要创建资源的主进程没有持久性 属性。服务器进程也可以 运行 在不同的机器上。

最喜欢的 意味着在工人之间共享资源:

  • 数据库 - 在需要可靠存储和可伸缩性的资源的持久性的情况下。示例:PostgreSQLMariaDBMongoDB 等等。
  • 缓存 (key/value) - 在数据的临时性质的情况下,比数据库更快,但不具有这种可扩展性并且通常不符合 ACID。示例:RedisMemcached

下面我将展示两个非常简单的示例,说明如何使用这两种方法在工作人员之间的 FastAPI 应用程序中共享数据。例如,我将 aiocache library with Redis as backend and Tortoise ORM library with PostgreSQL 作为后端。由于 FastAPI 是我选择基于 asyncio 的库的异步框架。

测试项目结构如下:

.
├── app_cache.py
├── app_db.py
├── docker-compose.yml
├── __init__.py

Docker-编写文件:

对于实验,您可以使用以下 docker-compose 文件,将 5432 (Postgres) 和 6379 (Redis) 端口暴露给 localhost.

version: '3'

services:
  database:
    image: postgres:12-alpine
    ports:
      - "5432:5432"
    environment:
      POSTGRES_PASSWORD: test_pass
      POSTGRES_USER: test_user
      POSTGRES_DB: test_db
  redis:
    image: redis:6-alpine
    ports:
      - "6379:6379"

开始:

docker-compose up -d

缓存(aiocache

Aiocache provides 3 main entities:

  • backends: Allow you specify which backend you want to use for your cache. Currently supporting: SimpleMemoryCache, RedisCache using aioredis and MemCache using aiomcache.
  • serializers: Serialize and deserialize the data between your code and the backends. This allows you to save any Python object into your cache. Currently supporting: StringSerializer, PickleSerializer, JsonSerializer, and MsgPackSerializer. But you can also build custom ones.
  • plugins: Implement a hooks system that allows to execute extra behavior before and after of each command.

开始:

uvicorn app_cache:app --host localhost --port 8000 --workers 5
# app_cache.py
import os
from aiocache import Cache
from fastapi import FastAPI, status


app = FastAPI()
cache = Cache(Cache.REDIS, endpoint="localhost", port=6379, namespace="main")


class Meta:
    def __init__(self):
        pass

    async def get_count(self) -> int:
        return await cache.get("count", default=0)

    async def set_count(self, value: int) -> None:
        await cache.set("count", value)

    async def increment_count(self) -> None:
        await cache.increment("count", 1)


meta = Meta()


# increases the count variable in the meta object by 1
@app.post("/increment")
async def increment():
    await meta.increment_count()
    return status.HTTP_200_OK


# returns a json containing the current count from the meta object
@app.get("/report")
async def report():
    count = await meta.get_count()
    return {'count': count, "current_process_id": os.getpid()}


# resets the count in the meta object to 0
@app.post("/reset")
async def reset():
    await meta.set_count(0)
    return status.HTTP_200_OK

数据库(Tortoise ORM + PostgreSQL)

开始: 为了简单起见,我们先运行一个worker在数据库中创建一个schema:

uvicorn app_db:app --host localhost --port 8000 --workers 1
[Ctrl-C] 
uvicorn app_db:app --host localhost --port 8000 --workers 5
# app_db.py
from fastapi import FastAPI, status
from tortoise import Model, fields
from tortoise.contrib.fastapi import register_tortoise


class MetaModel(Model):
    count = fields.IntField(default=0)


app = FastAPI()


# increases the count variable in the meta object by 1
@app.get("/increment")
async def increment():
    meta, is_created = await MetaModel.get_or_create(id=1)
    meta.count += 1  # it's better do it in transaction
    await meta.save()
    return status.HTTP_200_OK


# returns a json containing the current count from the meta object
@app.get("/report")
async def report():
    meta, is_created = await MetaModel.get_or_create(id=1)
    return {'count': meta.count}


# resets the count in the meta object to 0
@app.get("/reset")
async def reset():
    meta, is_created = await MetaModel.get_or_create(id=1)
    meta.count = 0
    await meta.save()
    return status.HTTP_200_OK

register_tortoise(
    app,
    db_url="postgres://test_user:test_pass@localhost:5432/test_db",  # Don't expose login/pass in src, use environment variables
    modules={"models": ["app_db"]},
    generate_schemas=True,
    add_exception_handlers=True,
)

您可以在不需要任何外部库或使用数据库等增加任何额外复杂性的情况下创建架构。

这将是我们用于跨不同进程共享对象的服务器。

from multiprocessing.managers import SyncManager


class MyManager(SyncManager):
    pass

syncdict = {}

def get_dict():
    return syncdict

if __name__ == "__main__":
    MyManager.register("syncdict", get_dict)
    manager = MyManager(("127.0.0.1", 5000), authkey=b"password")
    manager.start()
    input()
    manager.shutdown()

将此文件命名为 server.py,并在不同的进程中将其命名为 运行。只需 python server.py 就可以了。

让我们跳到客户端实现。

这将是我们的客户端实现。

from multiprocessing.managers import SyncManager
from typing import Optional, Dict, Any, Union


class MyManager(SyncManager):
    ...


class Meta:
    def __init__(self, *, port: int) -> None:
        self.manager = MyManager(("127.0.0.1", port), authkey=b"password")
        self.manager.connect()
        MyManager.register("syncdict")

        self.syndict = self.manager.syncdict()

    def update(self, kwargs: Dict[Any, Any]) -> None:
        self.syndict.update(kwargs)

    def increase_one(self, key: str) -> None:
        self.syndict.update([(key, self.syndict.get(key) + 1)])

    def report(self, item: Union[str, int]) -> int:
        return self.syndict.get(item)


meta = Meta(port=5000)

让我们将它与我们的 API 合并。

from fastapi import FastAPI, status

from multiprocessing.managers import SyncManager
from typing import Optional, Dict, Any, Union


class MyManager(SyncManager):
    ...


class Meta:
    def __init__(self, *, port: int, **kwargs: Dict[Any, Any]):
        self.manager = MyManager(("127.0.0.1", port), authkey=b"password")
        self.manager.connect()
        MyManager.register("syncdict")

        self.syndict = self.manager.syncdict()
        self.syndict.update(**kwargs)

    def increase_one(self, key: str):
        self.syndict.update([(key, self.syndict.get(key) + 1)])

    def reset(self, key: str):
        self.syndict.update([(key, 0)])

    def report(self, item: Union[str, int]):
        return self.syndict.get(item)


app = FastAPI()

meta = Meta(port=5000, cnt=0)

# increases the count variable in the meta object by 1
@app.get("/increment")
async def increment(key: str):
    meta.increase_one(key)
    return status.HTTP_200_OK


# returns a json containing the current count from the meta object
@app.get("/report")
async def report(key: str):
    return {"count": meta.report(key)}


# resets the count in the meta object to 0
@app.get("/reset")
async def reset(key: str):
    meta.reset(key)
    return status.HTTP_200_OK

我要启动我们 API 的两个实例,一个在 8000 上,另一个在 8001 上。

In: curl -X GET "http://127.0.0.1:8000/report?key=cnt"
Out: {"count": 0}

In: curl -X GET "http://127.0.0.1:8001/report?key=cnt"
Out: {"count": 0}

两者都以 0 值开始。现在让我们增加它

for _ in {1..10}; do curl -X GET "http://127.0.0.1:8000/increment?key=cnt" &; done

我 运行 端口 8000 上的 curl 10 次,这意味着 cnt 应该是 10.

让我们从8001端口检查一下:

In: curl -X GET "http://127.0.0.1:8001/report?key=cnt" 
Out: {"cnt": 10}

像魅力一样工作。

有两件事需要考虑。

  1. 您应该在不同的进程中启动您的应用程序。更具体地说,uvicorn my_app:app 和您的服务器不应是父进程。
  2. 您可能想要添加诸如正常关机之类的内容。由于这是一个非常简单但高度可扩展的示例。

如果您 运行 您的 FastAPI 服务使用 gunicorn 和 uvicorn 的设置,如 docs you can employ the method 中所述,以更简单的方式。您可以将 gunicorn 的 --reload 设置与 multiprocessing.Manager 结合使用,以避免启动另一个服务器的必要性。特别是以下内容无需额外设置即可在单个 Docker 容器中运行。

import logging
from multiprocessing import Manager

manager = Manager()

store = manager.dict()

store["count"] = 0

from fastapi import FastAPI

app = FastAPI()


@app.post("/increment")
async def increment():
    store["count"] = store["count"] + 1


@app.get("/count")
async def get_count():
    return store["count"]


@app.on_event("startup")
async def startup_event():
    uv_logger = logging.getLogger("uvicorn.access")
    handler = logging.StreamHandler()
    handler.setFormatter(
        logging.Formatter(
            "%(process)d - %(processName)s - %(asctime)s - %(levelname)s - %(message)s"
        )
    )
    uv_logger.addHandler(handler)

通过(您需要 fastapi、guvicorn 和 uvicorn 库)将此保存为 demo.py 和 运行:

GUNICORN_CMD_ARGS="--bind=127.0.0.1 --workers=3 --preload --access-logfile=-" gunicorn -k uvicorn.workers.UvicornWorker demo:app

(这里--preload是必不可少的!)

尝试通过位于 http://localhost:8000/docs 的 OpenApi UI 进行递增,并将对 /count 端点的多次调用与访问日志输出中的进程 ID 进行比较,以查看它 returns 增加的值,无论哪个工作进程正在响应。

注意:我在这里不对线程/异步安全做出任何声明,并且这种方法可能不应该在生产服务中使用。如有任何疑问,您应该始终依赖适当的数据库/缓存/内存存储解决方案来进行生产设置。我自己只在演示代码中使用这个!