Skip to content

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

Discussion options

You must be logged in to vote

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 i…

Replies: 6 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by estebanx64
Comment options

Kludex
Sep 2, 2021
Collaborator Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

Kludex
Sep 2, 2021
Collaborator Sponsor

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@kevinddchen
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
5 participants
Converted from issue

This discussion was converted from issue #3854 on February 27, 2023 22:28.