Use Protobuf With Fastapi

Protocol buffers, also known as Protobuf, are a popular data serialization format used for communication between services. They are efficient, easy to use, and language-agnostic. In this article, we will look at how to use Protobuf with FastAPI, a modern, high-performance web framework for building APIs with Python. First, let’s start by installing the necessary dependencies. You will need to install fastapi, google-protobuf, and grpcio. You can do this by running the following command: ...

January 4, 2023 · 2 min · 374 words

Understand Async Await in Python

Async/await is a powerful programming construct that allows you to write asynchronous code in a synchronous-like style. It was introduced in Python 3.5 as part of the asyncio module and has become a popular choice for writing concurrent and parallel code in Python. In this post, we’ll take a look at what async/await is and how it works, as well as some of the benefits and drawbacks of using it. We’ll also see some examples of how to use async/await in Python to write efficient and scalable code. ...

January 1, 2023 · 5 min · 1030 words

Create a Webserver With Fastapi and Uvicorn

FastAPI is a modern, fast, web framework for building APIs with Python 3.7 and above. It is built on top of Starlette, a lightweight ASGI framework, and uses the uvicorn ASGI server. Here is an example of how to create a web server with FastAPI and uvicorn: Install FastAPI and uvicorn using pip: pip install fastapi uvicorn Create a file called main.py and import FastAPI: from fastapi import FastAPI app = FastAPI() Define a function that will be the endpoint for your API. This function should have a request parameter that specifies the HTTP request and a response parameter that specifies the HTTP response. You can use the @app.get decorator to define a function as a GET endpoint: @app.get("/") def read_root(request, response): response.status_code = 200 return {"Hello": "World"} Run the web server using uvicorn: if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000) This will start the web server on the specified host and port (in this case, 0.0.0.0 and 8000). You can then access the endpoint at http://0.0.0.0:8000/. ...

December 30, 2022 · 1 min · 186 words

What Does Yield Keyword Do in Python

The keyword “yield” is a important part of the Python programming language, and it can be used in a number of different ways. In this article, we’ll take a closer look at what the yield keyword does in Python, and how it can be used to create more efficient and powerful programs. The primary use of the yield keyword is to create a generator function. A generator function is a special type of function that produces a sequence of values, one at a time, when it is called. Unlike a regular function, which executes all of its code and returns a single value, a generator function can be paused at any point and resumed later, allowing it to produce a potentially infinite sequence of values. ...

December 29, 2022 · 3 min · 440 words

Poetry No Module Seed via App Data

For my last project, I used poetry to manage dependencies and package my code. Poetry comes with all the tools you might need to manage your projects in a deterministic way. Anyway, after running poetry init in an existing project, I tried to let poetry create a virtuale environment, by adding the first dependecy, but when I did poetry add twisted it suddenly hang out, printing: No module named 'virtualenv.seed.via_app_data' Why is that? Because poetry was using the wrong virtualenv instance, so to solve this: ...

November 1, 2021 · 1 min · 117 words