为什么 API 不执行循环任务 - FastAPI、Celery?
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论