This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Await dont work in BackgroundTaks (Fastapi, Beanie, MongoDB) #5403
Comments
You are not calling any function inside background Task.
|
|
Sorry i forgot, insiste o calling the function upside filter_audits
El El vie, 16 de sep. de 2022 a la(s) 14:39, Jarro van Ginkel <
***@***.***> escribió:
background_tasks.add_task() doesn’t add any task.
—
Reply to this email directly, view it on GitHub
<#5403 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHFJEFNS5FQTBWSZ2JGOQH3V6SWGDANCNFSM6AAAAAAQOQK35E>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
--
Lea Guidi.
|
the code is correct now, sorry for my English. |
I cannot reproduce this using the following example: import asyncio
from fastapi import BackgroundTasks, FastAPI
app = FastAPI()
async def get_items_from_db():
print("Get some items from DB..")
await asyncio.sleep(1)
return [1, 2, 3, 4]
async def background_task_function():
print("Background task is executing..")
result = await get_items_from_db()
print(f"Items collected: {result}")
@app.get("/")
async def root(bt: BackgroundTasks):
print("Endpoint is called, now adding BG task..")
bt.add_task(background_task_function)
print("BG task added, returning response..")
return {"hello": "world"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) The above example will print correctly when calling
The issue might be the actual call to
But to answer your issue title: you can certainly use |
could be possible, really i don't try to use another thing with await, the
problem are in that line (Audit.find()) but, when i use another version of
fastapi (0.78.0) works fine, the problem is finding in a mongodb database
with beanie and fastapi version 0.85.0.
El sáb, 17 sept 2022 a la(s) 12:29, Jarro van Ginkel (
***@***.***) escribió:
… I cannot reproduce this using the following example:
import asynciofrom fastapi import BackgroundTasks, FastAPI
app = FastAPI()
async def get_items_from_db():
print("Get some items from DB..")
await asyncio.sleep(1)
return [1, 2, 3, 4]
async def background_task_function():
print("Background task is executing..")
result = await get_items_from_db()
print(f"Items collected: {result}")
@app.get("/")async def root(bt: BackgroundTasks):
print("Endpoint is called, now adding BG task..")
bt.add_task(background_task_function)
print("BG task added, returning response..")
return {"hello": "world"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
The issue might be the actual call to Audit. Try the following:
- Print something before you call Audit.find(), to assert that the
background task is actually executing.
- Try to ensure that the Audit.find() actually returns something.
But to answer your issue title: you can certainly use await in a
background task, and it should work. My money would be on the Audit.find()
doesn't return anything.
—
Reply to this email directly, view it on GitHub
<#5403 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHFJEFK2LBPMYN25ROEUHLTV6XPVLANCNFSM6AAAAAAQOQK35E>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
I am using versión 0.85.0 no 0.58, i write that tris error no occurs no
versión 0.78.0
El El sáb, 17 de sep. de 2022 a la(s) 18:49, Irfanuddin Shafi Ahmed <
***@***.***> escribió:
0.58 is very old. Please update FastAPI :/
—
Reply to this email directly, view it on GitHub
<#5403 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHFJEFPFHKQDMSZLN2XN5FLV6Y4IFANCNFSM6AAAAAAQOQK35E>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
--
Lea Guidi.
|
There are people on our discord server that can help you in Spanish: https://fastapi.tiangolo.com/help-fastapi/?h=discord#join-the-chat. |
@leaguidi if you use BaseHTTPMiddleware in your project then it may affect your BackgroundTasks, take a look at issues mentioned in encode/starlette#1715 (comment) |
@leaguidi @vanishingESCkey a fix for encode/starlette#1715 (comment) has been released in https://github.com/encode/starlette/releases/tag/0.21.0 Watch PR #5471 |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
First Check
Commit to Help
Example Code
Description
when you call de endpoint return the result {"hash":"12345678"} but the background task dont print the result founded on mongodb.
I realized that the error occurs with version 0.85.0 but it works fine with version 0.78.0
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.85.0
Python Version
3.10.9
Additional Context
No response
The text was updated successfully, but these errors were encountered: