question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

got Future <Future pending> attached to a different loop

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kludexcommented, Sep 2, 2021

Yeah, that PR solves an issue that should never happen. 😃

0reactions
ResetXDcommented, Sep 3, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Using queues results in asyncio exception "got Future ...
run() , so they use events.get_event_loop() . asyncio.run() creates a new loop, and futures created for the queue in one loop can't then...
Read more >
Task got Future <Future pending> attached to a different loop
I'm trying to reproduce the simplest example with Python native module with async function exported and call it from Python. lib.rs: use ...
Read more >
tiangolo/fastapi - Gitter
@david-e you're most likely hit by the infamous got Future <Future pending> attached to a different loop. well this is the case, if...
Read more >
על השגיאה got Future attached to a different loop ב Python
על השגיאה got Future attached to a different loop ב Python ... <Task pending name='Task-4' coro=<download() running at post.py:6> cb=[_wait.<locals>.
Read more >
Futures — Python 3.11.1 documentation
Return the event loop the Future object is bound to. New in version 3.7. This example creates a Future object, creates and schedules...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found