title: FastAPI and Python threads
body: <p>I have a little issue with FastAPI and additional threads spawning. Let's say i have an application that serves two endpoints.</p>
  <ol>
  <li>One of them to <code>/create_user</code> to create user in some database</li>
  <li>Other is just <code>/ping</code>. Ping is made simply because my app is running in Kubernetes and it's continuously checking if my app is alive by sending GET request and receive response_code 200.</li>
  <li>Additionally i have a separate process made with <code>threading.Thread</code>, that receive some key from external service. Key have TTL, so it needs to be renewed from time to time.</li>
  </ol>
  <p>The problem is when i load data by first endpoint, the database i'm loading into, is pretty slow and can answer up to 10 seconds. In that moment all other endpoints(including <code>/ping</code>) is locked. So k8s is thinking that my app is dead and try to rollback it.</p>
  <p>I could simply try to increase the number of workers, that serve the application with command <code>uvicorn main:app --workers 4</code>
  BUT additional thread is also spawning with each worker and output in logs looks smth like that</p>
  <pre><code>INFO:     Application startup complete.
  Hello from additional thread
  INFO:     Started server process [88030]
  Hello from additional thread 
  INFO:     Waiting for application startup
  Hello from additional thread Hello from additional thread
  INFO:     Application startup complete. Hello from additional thread
  </code></pre>
  <p>My question <em>is it possible to spawn only one additional thread with multiple gunicorn workers?</em></p>
  <p>Here is code snippet from my main.py</p>
  <pre><code>@app.post(&quot;/api/v1/create_user&quot;)
  async def create_user() -&gt; JSONResponse:
      &quot;&quot;&quot;Some creation magic here&quot;&quot;&quot;
      return JSONResponse(status_code=status.HTTP_201_CREATED, content={&quot;Success&quot;: True,                                                                     &quot;Username&quot;:raw_credentials[&quot;user&quot;]})
      
      
  @app.get(&quot;/ping&quot;, status_code=status.HTTP_200_OK)
  async def dummy_response():
      return
  
  # Special treads lunching for some jobs that need to be repeated during app lifecycle.
  t1 = Thread(target=renew_api_token)
  t1.start()
  </code></pre>