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.

[QUESTION] How do I start a thread with FastApi uvicorn

See original GitHub issue

Description I tried to integrate the uvicorn with FastApi inside my application having uvicorn run as a thread like I do when I’m was using flask (that could run as a thread while the main app was the main thread).

But I get the error: ValueError: signal only works in main thread

How can I get rid of the error? To have the main application run while the web server part run as another thread?

Well to be fair I got a work around and I run the main application as a thread but … are there alternatives to fix the issue in a proper way and make the sample code below run.,

Additional context Here is the sample source code:

import threading
import uvicorn
from fastapi import FastAPI

app = FastAPI()

def start_web_app():
    uvicorn.run(app, port=5000, log_level="info")

@app.get("/")
def read_root():
    return {"Hello": "Word"}

if __name__ == "__main__":
    thread_fastapi = threading.Thread(name='Web App FastApi', daemon=True, target=start_web_app)

    print("star thread FastApi")
    thread_fastapi.start()
    print("ended thread")
    ...

Here is the full error that I get:
Connected to pydev debugger (build 191.8026.44)
email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
star thread FastApi
ended thread
Exception in thread Web App FastApi:
Traceback (most recent call last):
  File "D:\Git\sms_rates\venv\lib\site-packages\uvicorn\main.py", line 455, in install_signal_handlers
    loop.add_signal_handler(sig, self.handle_exit, sig, None)
  File "C:\Tools\Python37-32\lib\asyncio\events.py", line 540, in add_signal_handler
    raise NotImplementedError
NotImplementedError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Tools\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Tools\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "D:/Git/sms_rates/test_thread_uvicorn.py", line 8, in start_web_app
    uvicorn.run(app, port=8449, log_level="info")
  File "D:\Git\sms_rates\venv\lib\site-packages\uvicorn\main.py", line 279, in run
    server.run()
  File "D:\Git\sms_rates\venv\lib\site-packages\uvicorn\main.py", line 307, in run
    loop.run_until_complete(self.serve(sockets=sockets))
  File "C:\Tools\Python37-32\lib\asyncio\base_events.py", line 568, in run_until_complete
    return future.result()
  File "D:\Git\sms_rates\venv\lib\site-packages\uvicorn\main.py", line 319, in serve
    self.install_signal_handlers()
  File "D:\Git\sms_rates\venv\lib\site-packages\uvicorn\main.py", line 459, in install_signal_handlers
    signal.signal(sig, self.handle_exit)
  File "C:\Tools\Python37-32\lib\signal.py", line 47, in signal
    handler = _signal.signal(_enum_to_int(signalnum), _enum_to_int(handler))
ValueError: signal only works in main thread

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
zaidyahyacommented, Mar 25, 2021

@tincumagic looking into a similar solution. What was your work around? Did you manage to run the application in main thread and server in another?

6reactions
tiangolocommented, Oct 30, 2019

Hmm, you shouldn’t run it in a thread.

FastAPI will handle it’s own thread pool when necessary (depending on if you use async def or def).

And either way, it will handle requests in the async event loop.

Running a WSGI framework (like Flask) in threads is just a trick to increase concurrency, handled by the OS.

With an async framework, concurrency is a top level feature, everything is built around that, and is handled directly in the language, in the event loop. That’s why it can have better performance.

Try first following the docs as they are first. That should work well without extra effort. Running Uvicorn in it’s own process.

If you need something more “robust” or need to optimize performance, check the Docker image, it has everything pre-fine-tuned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FastAPI python: How to run a thread in the background?
Try moving the thread stuff to before the run(). It's possible the run() doesn't return until the server dies. · doesn't work as...
Read more >
Server Workers - Gunicorn with Uvicorn - FastAPI
Here I'll show you how to use Gunicorn with Uvicorn worker processes. ... Then Gunicorn would start one or more worker processes using...
Read more >
tiangolo/fastapi - Gitter
Hi @jasiek, that'd totally solve our problem! ... @singhjan49 uvicorn may create multiple processes of your api so you will need multiprocess/system-wide ...
Read more >
Concurrency with FastAPI - Medium
In one of my earlier tutorials, we have seen how we can create a Python application using FastAPI. To know more you can...
Read more >
Concurrency in Python with FastAPI - DEV Community ‍ ‍
Run it with. uvicorn server:app --reload. You should see at http://127.0.0.1:8000/wait something like: { "duration": 1 }. Ok, it works.
Read more >

github_iconTop Related Medium Post

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