FastAPI is built on top of Starlette for the web parts and Pydantic for data validation. It leverages Python type hints to automatically validate, serialize, and deserialize data. This not only makes the code more readable but also helps in generating interactive API documentation automatically. FastAPI uses an asynchronous programming model, but it also supports synchronous code, making it easy for developers who are new to asynchronous programming.
Tornado is an open - source Python web framework and asynchronous networking library. It is designed to handle a large number of concurrent connections with low latency. Tornado uses a non - blocking I/O model and an event - driven architecture. This means that it can handle multiple requests simultaneously without waiting for each request to complete before moving on to the next one. Tornado is well - suited for applications that require real - time communication, such as chat applications and long - polling APIs.
Here is a simple example of creating a basic API using FastAPI:
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
To run this application, you can use uvicorn
, a lightning - fast ASGI server. Save the above code in a file named main.py
and run the following command in the terminal:
uvicorn main:app --reload
Here is a simple example of creating a basic web application using Tornado:
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
To run this application, simply save the code in a file named tornado_app.py
and run it using Python:
python tornado_app.py
/docs
and the ReDoc at /redoc
to test and explore your API.write_error
method in the RequestHandler
class to customize the error response.pytest
along with the TestClient
provided by FastAPI to test your endpoints.Both FastAPI and Tornado are powerful Python web frameworks, but they are suited for different use cases. FastAPI is a great choice for building modern APIs with automatic documentation, data validation, and easy - to - use dependency injection. It is a good option for developers who want to quickly build APIs with Python and take advantage of the latest Python features.
Tornado, on the other hand, is well - suited for applications that require high - performance and real - time communication. Its non - blocking I/O model and event - driven architecture make it ideal for handling a large number of concurrent connections with low latency.
When choosing between FastAPI and Tornado, consider the requirements of your project, such as the type of application you are building, the expected number of concurrent connections, and the complexity of the data validation and business logic.