title: Future task attached to a different loop
body: <p>I am trying to connect to mongodb in FastAPI. I am repeatedly getting this exception.</p>
  <p>File - main.py</p>
  <pre><code>app = FastAPI(
      title=config.PROJECT_NAME, docs_url=&quot;/api/docs&quot;, openapi_url=&quot;/api&quot;
  )
  
  @app.get(&quot;/api/testing&quot;)
  async def testit():
      user_collection = readernetwork_db.get_collection(&quot;user_collection&quot;)
      all_users = await user_collection.find_one({&quot;email&quot;: &quot;sample_email&quot;})
      print(&quot;all users --- &quot;, all_users)
      return all_users
  
  if __name__ == &quot;__main__&quot;:
      uvicorn.run(&quot;main:app&quot;, host=&quot;0.0.0.0&quot;, reload=True, port=8888)
  </code></pre>
  <p>File - session.py</p>
  <pre><code>import motor.motor_asyncio
  from app.core import config
  
  
  print(&quot;here we go again....&quot;)
  client = motor.motor_asyncio.AsyncIOMotorClient(
      config.MONGOATLAS_DATABASE_URI)
  readernetwork_db = client.get_database(&quot;readernetwork&quot;)
  </code></pre>
  <p>Exception -:</p>
  <pre><code>all_users = await user_collection.find_one({&quot;email&quot;: &quot;sample_email&quot;})
  
  RuntimeError: Task &lt;Task pending name='Task-4' coro=&lt;RequestResponseCycle.run_asgi() running at /usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py:389&gt; cb=[set.discard()]&gt; got Future &lt;Future pending cb=[_chain_future.&lt;locals&gt;._call_check_cancel() at /usr/local/lib/python3.8/asyncio/futures.py:360]&gt; attached to a different loop
  </code></pre>
  <p>I don't know where I am getting this wrong. Should I specify a event loop to motor?</p>