Skip to content

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

got Future <Future pending> attached to a different loop #3854

Closed
9 tasks done
krishsharma0413 opened this issue Sep 2, 2021 · 5 comments
Closed
9 tasks done

got Future <Future pending> attached to a different loop #3854

krishsharma0413 opened this issue Sep 2, 2021 · 5 comments
Labels
question Question or problem question-migrate

Comments

@krishsharma0413
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

def main():
  uvicorn.run(app,host="0.0.0.0",port=8090,loop="asyncio")
loop = asyncio.get_event_loop()
def run():
  loop.run_until_complete(main())

def keep_alive1():
    t = threading.Thread(target=run)
    t.start()

keep_alive1()


client.run('discord bot token')

Description

i was using discord client with fastapi i used another thread for running uvicorn.
the bot worked fine but when i used the fastapi endpoints it gave me an error

RuntimeError: Task <Task pending name='Task-32' coro=<RequestResponseCycle.run_asgi() running at /opt/virtualenvs/python3/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py:369> cb=[set.discard()]> got Future <Future pending> attached to a different loop

the endpoint that gave me this error was asynchronous using mongodb module motor, other non asynchronous endpoints worked fine like the /docs.

when i removed the discord client with just the fastapi it still didnt worked with uvicorn.run() but did worked with the cmd command uvicorn main:app not sure why tho.

Operating System

Windows

Operating System Details

No response

FastAPI Version

0.66.0

Python Version

3.9.4

Additional Context

No response

@krishsharma0413 krishsharma0413 added the question Question or problem label Sep 2, 2021
@frankie567
Copy link
Contributor

This is a quite common issue when using Motor (unfortunately, their implementation doesn't always play well with some event loops patterns). I wrote some details about this some time ago here: fastapi-users/fastapi-users#663 (reply in thread)

When using Motor, if you want to invoke Uvicorn programmatically you have to make sure of two things:

  1. Put the invokation in a separate file (like main.py) alone, not besides the app instance;
  2. Don't import app to pass it to uvicorn: prefer the import path as a string. This way, you'll ensure Uvicorn imports the module by itself with the right loop attached:
# main.py

import uvicorn

if __name__ == "__main__":
    # Assuming your `app` variable is in a module named `app.py`
    uvicorn.run("app:app", host="0.0.0.0", port=8000)

BTW, I see in your example that you do loop.run_until_complete(main()) on the function that calls Uvicorn. You don't need to do this (see above), Uvicorn takes care of this.

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Sep 2, 2021

Is this related to encode/uvicorn#1110 ?

@frankie567
Copy link
Contributor

From what I understand of the issue, yes, this is what probably causes the problem.

However, I also think that Motor could greatly improve things on their side: contrary to most database drivers, they don't provide a connect/disconnect mechanism we could trigger on FastAPI startup; which would be then in the right loop context.

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Sep 2, 2021

Yeah, that PR solves an issue that should never happen. :)

@krishsharma0413
Copy link
Author

thanks it worked, tho you should add a little note in the docs about the motor connector.

@tiangolo tiangolo reopened this Feb 27, 2023
Repository owner locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6566 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

4 participants