Developing a blazing fast website in Python using Falcon and Bjoern

khalid Web Development 0 Comments

Few days back, I wanted to develop a web service in Python. I wanted to run the service on my VPS with as minimal resources as possible while providing a fast service. I started looking for benchmarks online of different Python web frameworks and WSGI servers, and found out about two awesome libraries, falcon and bjoern.

Falcon is a micro-framework that is designed for building very fast micro-services. It is very flexible as it permits you to install add-ons and other libraries (e.g. Jinja2) easily. Just what I was looking for. On the other hand, Bjoern is a “A screamingly fast, ultra-lightweight WSGI server”, as their Github page says. It uses around 600KB of memory. 600KB, imagine how light it is!

In this post, I will explain how to setup and use Falcon with Bjoern for serving efficient websites 🙂

Installation

I will assume that you have Python, either 2.7 or 3, installed. If you don’t, install it before proceeding with the tutorial. Let the fun begin!

Installing Falcon and developing your service

The first thing to do is to install Falcon, follow the installation steps from falcon’s website. Don’t proceed with installing any WSGI servers as we will use bjoern. Once you have installed falcon, implement your falcon service by creating Resources and link them to your Falcon app. For more details, follow the tutorial provided by falcon.


Hosting your app with Bjoern

To host your website with Bjoern, you need to install “libev-dev” as it depends on it. On Ubuntu and Debian, run the below command to install the library.

Now, you have the system requirements setup. What we will do next is create a file for serving the service where we attach the service app (Falcon’s API) to Bjoern. Name the file something like “app.py” and copy-paste the below code to the file while changing Falcon’s resources to yours.

When the code above is executed, it will attach your falcon’s app to bjoern server by binding it to either a TCP IP and port or to a Linux socket. You can select the type of binding by using the parameters -ip and -socket. By default, this script will bind the app to all connections from the port 8000.

Before proceeding to mapping a domain to our bjoern server, let’s try that the service works locally by executing the below command and visiting http://127.0.0.1:8000.

Pointing a domain name to Bjoren server

You have reached to this point, great! You are almost done configuring your robust web service. In this post, I will use Apache and define a proxy pass to the service from requests coming to a given domain. Alternatively, you can achieve the same with Nginx.

To install apache2, follow this tutorial. Then, fire up the terminal and run the below commands to install and activate Apache’s proxy mod.

To create a virtual host and point a domain to it, create a file in “/etc/apache2/sites-available/” and name it to your desired domain name with .conf as extension. Next, write the configuration below to it with configuring the ServerName, IP:Port, and Log paths.

After you have done that, execute sudo a2ensite conf-file.conf  in the terminal followed by sudo service apache2 restart . Hopefully nothing went wrong and everything ran smoothly.

Now, we need to create an auto-start service so that our web service is always alive. To do so, create a file named something like “service-name.service” under “/lib/systemd/system/” and give it 644 permission.

Thereafter, let’s define the service we want to run by the system by declaring the below settings in “service-name.service” file. Change the settings to match your setup.

Below are some useful systemctl commands to assist you in setting up and running the service.

Conclusion

In conclusion, we have seen how to write, configure and run an insanely fast web service using Falcon and Bjoern. Using Apache and proxy passing the requests through it might slow down the requests a bit. However, I haven’t experimented with other alternatives.

Happy coding and serving efficient web sites. 🙂

* (This post contains affiliate links. Please read my disclosures for more details).

Share this Post