Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Nana
Nana

Posted on

Tornado 🌪️ and Load Balancing Tutorial

This tutorial will guide you through setting up the Tornado web framework, creating a simple "Hello, world" application, and implementing load balancing using NGINX. 🌪️

Tornado Web Framework

Tornado is a Python web framework and asynchronous networking library originally developed by FriendFeed. It leverages non-blocking network I/O, allowing it to scale to handle numerous open connections. This makes it ideal for tasks such as long polling, WebSockets, and applications requiring persistent connections for each user. You can find more information in the officialdocumentation.

Installation

Installing Tornado is straightforward usingpip3:

pip3 install tornado

Hello World Application

Let's start by creating a basic "Hello, world" application using Tornado.

  1. Create a file namedHelloWorld.py and add the following code:
importtornado.ioloopimporttornado.webclassMainHandler(tornado.web.RequestHandler):defget(self):self.write("Hello, world")defmake_app():returntornado.web.Application([(r"/",MainHandler),])if__name__=="__main__":app=make_app()app.listen(8888)tornado.ioloop.IOLoop.current().start()
Enter fullscreen modeExit fullscreen mode
  1. Run the application in your terminal:

python3 HelloWorld.py

  1. Access the application in your browser athttp://localhost:8888.

Load Balancing with Tornado and NGINX

Understanding Load Balancing

Load balancing is a technique used in computer networking to distribute incoming network traffic across multiple servers. The primary goal of load balancing is to ensure optimal utilization of resources, enhance performance, and maintain high availability for web applications.

By distributing traffic across multiple servers, load balancing prevents any single server from becoming overwhelmed by a large number of requests, leading to improved response times and reduced downtime.

Balancing Multiple Servers

Now, let's delve into load balancing by creating multiple instances of the server using Tornado.

  1. Create a file namedindex.py and add the following code:
importtornado.ioloopimporttornado.webimportsysimportosclassbasicRequestHandler(tornado.web.RequestHandler):defget(self):self.write(f"Served from{os.getpid()}")if__name__=="__main__":app=tornado.web.Application([(r"/basic",basicRequestHandler)])port=8882if(sys.argv.__len__()>1):port=sys.argv[1]app.listen(port)print(f"Application is ready and listening on port{port}")tornado.ioloop.IOLoop.current().start()
Enter fullscreen modeExit fullscreen mode
  1. Run the application in your terminal:

python3 index.py

  1. Access the application in your browser athttp://localhost:8882/basic.

  2. To explore load balancing, run the application on different ports:

python3 index.py 1111 python3 index.py 2222 python3 index.py 3333

You'll notice that the server number changes as you access the URL in different browsers.

Using NGINX for Load Balancing

To automate the process of load balancing, we will utilize NGINX.

Installation

Install NGINX based on your operating system. Detailed instructions can be found in the officialdocumentation.

After installation, start NGINX using:

sudo nginx

Writing the Configuration File

Create a new file namedpython.conf and define the servers, ports, and URLs for NGINX to balance:

upstreampythonweb{serverlocalhost:1111;serverlocalhost:2222;serverlocalhost:3333;}server{listen80;location /basic{proxy_pass"http://pythonweb/basic";    }}
Enter fullscreen modeExit fullscreen mode

Explanation ofupstream: Anupstream defines a "cluster" to which you can send proxy requests. It's commonly used to define a cluster of web servers for load balancing or a cluster of application servers for routing/balancing.

Configuring NGINX

Navigate to the NGINX configuration files:

bashCopy code

cd /usr/local/etc/nginx/

Open the filenginx.conf.default. Add the lineinclude /path/to/folder/with/python.conf below the lineinclude servers/*;.

Note: Be careful not to modify other parts of the NGINX configuration.

After making the configuration changes, reload NGINX settings:

sudo nginx -s reload

Load Balancer Ready

Your load balancer is now ready. Run the Tornado application using:

python3 index.py

Access the application in your browser athttp://localhost:8882/basic. You will observe different server numbers, indicating successful load balancing.

By following these steps, you've set up a basic Tornado web application and implemented load balancing using NGINX. This tutorial provides a starting point for exploring more advanced features and configurations of both Tornado and NGINX.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Quality Engineer at @Ansible by @RedHat | #OpenSource | Diversity and Inclusion | #BlackTech ✊🏾| Autist | They/Them
  • Location
    Ireland
  • Work
    Quality Engineer at Red Hat Inc
  • Joined

More fromNana

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp