Utilizing WebSockets, Queue and APScheduler for batch processing in FastAPI

Image for post
Image for post
Photo by Mihály Köles on Unsplash

In this tutorial, I am going to share a simple experiment that I have conducted over the weekend. The basic idea is to queue up all the incoming requests and process them in batches at certain interval each time. To do so, we are going to

  1. establish two-way connections between the clients and our server via WebSockets.
  2. store the incoming requests in queue
  3. run a scheduler and process the requests in batches
  4. return the response to users via WebSockets

The architecture is fairly straightforward and good enough for our case study. Please be reminded that if you are looking for production-ready solutions, you should be using RabbitMQ (message broker) or Celery (distributed task queue) instead based on your use cases. …


One simple trick to make your life easier

Image for post
Image for post
Photo by Sai Kiran Anagani on Unsplash

By reading this piece, you will learn to create a bash script that automates the restart process for your FastAPI server (production). As you have known, you can restart the server by using the reload argument during development.

uvicorn myapp:app --reload

When FastAPI (uvicorn ASGI) detects changes in the files, it will trigger a reload for you. However, you should not use the reload argument for the production server. This is simply because it will hog your CPU resources in order to detect changes to the local files.

Most of the time, you are going to deploy it using nohup and & argument which allows the process to continue running in the background. As a result, there might arise situations in which you just want to restart FastAPI…


10000 to 10K | 999999 to 1M | 1234567890 to 1.23B

Image for post
Image for post
Photo by Andrew Buchanan on Unsplash

By reading this piece, you will learn to format an integer (123456) into its own string representation (123K) in Python. This helps to improve the user experience in your application as displaying the number as string representation is a lot more coherent to the users. In this tutorial, I am going to showcase two different implementations:

  • a modified version of the codes shared on Stack Overflow
  • my own custom implementation

Having said that, this article does not cover string representation that is less than 0 such as micro and nano. …


Build a monitoring dashboard with AJAX, Promise and React Hooks

Image for post
Image for post
Image by Author

By reading this piece, you will learn to create a simple React-based monitoring dashboard which shows the air quality information based on a city selected by the users. Here is an example of the monitoring dashboard.


5 useful tips I wish I had known earlier

Image for post
Image for post
Photo by Alvaro Reyes on Unsplash

By reading this article, you will learn about a few important tricks which help to manage your expectations when integrating with any external third-party APIs. A while ago, I have gone through a painful integration process when dealing with APIs provided by an external vendor. I am a lot wiser now and would like to share the lessons that I have learned from it.

Some of the tips are based on Python implementations using the standard Python libraries. Besides, there will be JavaScript implementations as well for simple use cases. …


Save it locally via hyperlink or sending it as blob file to a remote server

Image for post
Image for post
Photo by Hyeonji Im on Unsplash

By reading this piece, you will learn to save a drawing on the HTML5 canvas element of your web application as an image. In this tutorial, I will be showcasing two methods to save it.

The first method is to store the image data inside a hyperlink (using the <a> tag). Then, you can download it manually from your browser via a manual click or trigger it programmatically using JavaScript. This option is good if you intend to let the users download it on their machine or just wanted to save a few images from the canvas.

As for the second method, I am going to convert the canvas as blob data and send it via FormData to a remote server. After that, you can easily save it on your server. This method is preferred if you are going to do data collection or saving a large amount of images from time to time. …


Choosing the right data type for your application

Image for post
Image for post
Image by Author

The topic for today is sets and hashes in Redis. These are two of the most useful data types in Redis and they’re often neglected by developers. It’s important to understand the fundamental concepts behind them because choosing the right data types helps a lot when trying to scale your application.

In this tutorial, I’m using Redis-Py to showcase the functionalities. The concept behind it is the same whichever programming language you use. Redis-Py uses the same name for function call as the original comand, with some minor exceptions. …


A curated list of nine lesser-known Python packages for your machine learning projects

Image for post
Image for post
Photo by Markus Winkler on Unsplash

This article will showcase a list of curated Python packages that are meant for machine learning. Over the past few years, there have been quite a number of amazing repositories specialized on machine learning in Github. Hence, I have compiled a list of 9 useful packages that are in active development. The last package release must be within a year from the time of this writing.

It is highly recommended to check them out regardless if you are a beginner or an expert in machine learning as you might find one that is useful to your projects. …


Step-by-step guide to receive files and save them locally

Image for post
Image for post
Image by Author

The topic for today is on saving images or text files that users have uploaded to your FastAPI server locally in disk. If you have used Flask before, you should know that it comes with its own built-in file.save function for saving files. Unfortunately, such feature is not present in FastAPI at the time of this writing. In this tutorial, you will learn to implement this functionality based on your own use cases.

Single File

For your information, the bare minimum code for a simple FastAPI server that accepts an image or a file uploaded via FormData is as…


Explore a new way to build your own contextual chatbots

Rasa Open Source logo
Rasa Open Source logo
Image by author. Rasa Open Source icon taken from Rasa Github.

Rasa Open Source is a machine learning framework used to build text- and voice-based chatbots. Recently, the team behind it successfully reached a notable milestone and released their first official version 2.0 on October 2020. The new version aims at unifying training data formats, configuration files, and the way to handle models. As a result, there are quite a number of major breaking changes compared to the previous version.

In this tutorial, we are going to dive deep into some of the major differences between Rasa 1.10 and the latest release candidate version. …

About

Ng Wai Foong

Senior AI Engineer@Yoozoo | Content Writer #NLP #datascience #programming #machinelearning | Linkedin: https://www.linkedin.com/in/wai-foong-ng-694619185/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store