Skip to main content

Simple FastAPI load balancer

Project description

Load Balancer

Simple FastAPI load balancer

pip install load-balancer

Usage

from multiprocessing import Process
from fastapi import FastAPI
import uvicorn
from load_balancer import session_affinity, # or round_robin

myapp = FastAPI()
# ...

NUM_WORKERS = 4
UDP_PATHS = [f'udp-{i}.sock' for i in range(NUM_WORKERS)]
balancer = session_affinity(UDP_PATHS)

procs = [Process(target=uvicorn.run, args=(balancer,))] + [
  Process(target=uvicorn.run, args=(myapp, path))
  for path in UDP_PATHS
]

for proc in procs:
  proc.start()

for proc in procs:
  proc.join()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

load_balancer-0.1.1.tar.gz (2.6 kB view hashes)

Uploaded Source

Built Distribution

load_balancer-0.1.1-py3-none-any.whl (3.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page