[QUESTION] How do I start a thread with FastApi uvicorn
See original GitHub issueDescription 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:
- Created 4 years ago
- Comments:7 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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?
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
ordef
).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.