为什么 API 不执行循环任务 - FastAPI、Celery?

发布于 2025-01-15 04:02:51 字数 1191 浏览 3 评论 0原文

我有一个 API,负责根据传递给它的地址构建数据库模型。这样的模型有数百个,我不想手动完成,因此我决定创建一个 API,在调用该 API 时,它将循环遍历所有地址并将它们传递给构建模型。

我使用: FastAPI、Celery

Celery 任务

from agents.walletReputation import WalletReputation

@app.task(name="walletReputation")
def wallet_reputation(id: str):
    WalletReputation(id).add_reputation_to_db()

    return {"message": "Success"}

生成器从 FB 生成项目

def all_addresses_generator():
    for row in session.query(DbNcTransaction).all():
        yield row

我的端点

# Make or update one wallet
@router.post("/run/{id}")
async def create_or_update(id: str):
    wallet_reputation.delay(id)

    return {"Status": "Task successfully add to execute"}

# Make or update all wallets
@router.post("/run/all")
async def create_or_update_all():
    for address in all_addresses_generator():
        wallet_reputation.delay(address)

    return {"Status": "Task successfully add to execute"}

你知道为什么吗第一个端点有效,而第二个端点不再有效?

I have an API that is responsible for building a database model based on the address passed to it. There are hundreds of such models, and I don't want to do it manually, so I decided to create an API that, when called, will loop through all the addresses and pass them to build the model.

I use: FastAPI, Celery

Celery task

from agents.walletReputation import WalletReputation

@app.task(name="walletReputation")
def wallet_reputation(id: str):
    WalletReputation(id).add_reputation_to_db()

    return {"message": "Success"}

Generator to yield item from FB

def all_addresses_generator():
    for row in session.query(DbNcTransaction).all():
        yield row

My endpoints

# Make or update one wallet
@router.post("/run/{id}")
async def create_or_update(id: str):
    wallet_reputation.delay(id)

    return {"Status": "Task successfully add to execute"}

# Make or update all wallets
@router.post("/run/all")
async def create_or_update_all():
    for address in all_addresses_generator():
        wallet_reputation.delay(address)

    return {"Status": "Task successfully add to execute"}

Do you have any idea why the first endpoint works and the second one doesn't anymore?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文