2022-10-27 16:49:51 +00:00
# We have an version of this using aiofile and aiofiles
# This is an sync version without any dependencies is normally much faster in CPython and PyPy3
2022-11-28 05:44:38 +00:00
# In production we highly recommend to use CDN like CloudFlare or/and NGINX or similar for static files (in any language/framework)
2022-10-27 16:49:51 +00:00
# Some performance data from my personal machine (Debian 12/testing, i7-7700HQ, 32GB RAM, Samsung 970 PRO NVME)
# using oha -c 400 -z 5s http://localhost:3000/
# nginx - try_files - 77630.15 req/s
2023-03-14 18:31:40 +00:00
# pypy3 - socketify static - 16797.30 req/s
# python3 - socketify static - 10140.19 req/s
2022-10-27 16:49:51 +00:00
# node.js - @fastify/static - 5437.16 req/s
# node.js - express.static - 4077.49 req/s
# python3 - socketify static_aiofile - 2390.96 req/s
# python3 - socketify static_aiofiles - 1615.12 req/s
# python3 - scarlette static uvicorn - 1335.56 req/s
# python3 - fastapi static gunicorn - 1296.14 req/s
# pypy3 - socketify static_aiofile - 639.70 req/s
# pypy3 - socketify static_aiofiles - 637.55 req/s
# pypy3 - fastapi static gunicorn - 253.31 req/s
2022-11-16 19:28:46 +00:00
# pypy3 - scarlette static uvicorn - 279.45 req/s
2022-10-27 16:49:51 +00:00
2022-11-16 19:28:46 +00:00
# Conclusions:
2022-11-28 05:44:38 +00:00
# With PyPy3 only static is really usable gunicorn/uvicorn, aiofiles and aiofile are really slow on PyPy3 maybe this changes with HPy
2022-12-09 14:14:29 +00:00
# Python3 with any option will be faster than gunicorn/uvicorn but with PyPy3 with static we got 4x (or almost this in case of fastify) performance of node.js
2022-12-09 14:12:22 +00:00
# But even PyPy3 + socketify static is 5x+ slower than NGINX
2022-10-27 16:49:51 +00:00
# Anyway we really recommends using NGINX or similar + CDN for production like everybody else
# Gunicorn production recommendations: https://docs.gunicorn.org/en/latest/deploy.html#deploying-gunicorn
# Express production recommendations: https://expressjs.com/en/advanced/best-practice-performance.html
# Fastify production recommendations: https://www.fastify.io/docs/latest/Guides/Recommendations/
2022-11-14 19:42:15 +00:00
from socketify import App , sendfile
2022-10-27 00:36:37 +00:00
app = App ( )
2022-11-16 19:28:46 +00:00
# send home page index.html
2022-10-27 00:36:37 +00:00
async def home ( res , req ) :
2022-11-16 19:28:46 +00:00
# sends the whole file with 304 and bytes range support
2022-10-31 23:51:16 +00:00
await sendfile ( res , req , " ./public/index.html " )
2022-11-16 19:28:46 +00:00
2022-10-27 00:36:37 +00:00
app . get ( " / " , home )
2022-11-16 19:28:46 +00:00
# serve all files in public folder under /* route (you can use any route like /assets)
2022-11-14 19:42:15 +00:00
app . static ( " / " , " ./public " )
2022-11-16 19:28:46 +00:00
app . listen (
3000 ,
lambda config : print ( " Listening on port http://localhost: %d now \n " % config . port ) ,
)
2022-10-27 00:36:37 +00:00
app . run ( )