Hi everyone. In this post, I'll introduce the Bocadillo framework. As you know, there are a lot of web frameworks in Python.
In the past days, I was looking for new technologies to improve myself. I saw Bocadillo on GitHub.
Bocadillo is a modern Python web framework filled with asynchronous salsa.
You can access Bocadillo's website using this link: https://bocadilloproject.github.io/
You can find Bocadillo on GitHub: https://github.com/bocadilloproject/bocadillo
Its key features are;
- Real-time capable
Transparent: All codes are developed with type-annotation.
You can also develop WebSocket apps. Actually, I think, its big claim is WebSockets.
The Bocadillo has a CLI tool to create projects. But you don't need to install the CLI tool.
pip install bocadillo-cli
pip install bocadillo
mkdir my_first_app cd my_first_app virtualenv . source bin/activate pip install bocadillo
Now, we installed the Bocadillo.
You can use favorite editor or IDE. I'll create a file called app.py
I'll edit app.py file with the nano editor. Yes this is hippie style :)
# app.py file from bocadillo import App app = App() @app.route("/") async def index(req, res): res.text = "Hello Dev Family"
That's all. Bocadillo highly similar to the Flask framework. To run our first app, we'll use the uvicorn.
Don't worry it comes up with the Bocadillo. You don't need to install it again.
uvicorn app:app --reload
We'll run our first application using the above command. After this command you'll see an output like below;
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process  INFO: Started server process  INFO: Waiting for application startup. INFO: ASGI 'lifespan' protocol appears unsupported.
This means, our web app works on port 8000. Good work, we created our first app. Let's change our app and use background tasks.
from asyncio import sleep from bocadillo import App app = App() @app.route("/") async def index(req, res): try: if req.query_params["wait"] == "true": @res.background async def waitForTest(): await sleep(15) print("I'll work :)") except: print(":/") res.text = "Hello Dev family"
Background tasks are a lightweight mechanism to perform processing after a request has been sent, without making the client wait for the result. Typical examples include sending email, or sending logs to a remote system.
When registered on the Response, the background task won't run immediately. Instead, the view will terminate as usual, and the response will be sent. Only then, the background task will execute.
This prevents clients from waiting for the background task to finish before getting a response.*
If you try without background tasks, you need to wait for the sleep method finish. Firstly you should try the above code.
1-) Open two terminals.
Run this command in the first terminal.
Immediately run this command in the second terminal.
You'll see "Hello Dev family" output in all terminals. You need also look at the original terminal which you run your first app.
After 15 seconds, you'll see "I'll work" output.
If you don't use async background tasks you will need to wait for the sleep method finish.
import time from bocadillo import App app = App() @app.route("/") async def index(req, res): try: if req.query_params["wait"] == "true": time.sleep(15) print("I'll work :)") except: print(":/") res.text = "Hello Dev family"
Don't forget, Background tasks must be non-blocking.. Because a background task is a coroutine, it must be non-blocking to avoid blocking the main thread and preventing requests from being processed.
That's all for now.
You should look at the Bocadillo Guide for the other features. I introduced the Bocadillo.
Thanks for reading.