High-performance ASGI framework
Project description
Aeros Framework Documentation
Aeros is a production-grade ASGI (Asynchronous Server Gateway Interface) package containing wrappers for widely used Web and API functions.
Features
- High-performance web server
- Async request handling
- Supports multi-threading
- Production-grade ASGI (async WSGI)
- In-Python code API
- Native server-side caching
- Native gzip compression
- Can be run in a separate thread
- Easy Framework based on Flask/Quart
- Custom global headers (like CORS etc.)
Why use Aeros over Flask and Quart?
While Flask is one of the most popular and frequently used frameworks, it doesn't come with a full WSGI server. Therefore, you will need an additional module like Waitress or Gunicorn. Quart shares the same features as Flask, but you can get more performance out of it, since it supports asynchronous request handling. But as for Flask, you will need a WSGI (an ASGI in this case) to deploy your Quart app into production. The most popular ASGI at the moment is called Hypercorn and is installed together with Quart.
But Hypercorn does only support deployment from console. Meaning, you will have to invoke
a start command like: hypercorn <file>:<app_variable_name>
to start your server. This
makes it hard to deploy a multi-thread web server.
Aeros combines all the benefits from Quart and Hypercorn, while maintaining the in-Python API, making it easy to create an application that can be run from custom code, not by shell.
A more detailed overview of pros and cons can be found here:
Feature | Aeros | Flask | Flask + Waitress | Flask + Gunicorn | Quart | Quart + Hypercorn | |
---|---|---|---|---|---|---|---|
In-Python API | |||||||
Easy to use | |||||||
Production-grade | |||||||
Asynchronous | |||||||
Multiple workers | |||||||
Callable from thread | |||||||
Native caching | |||||||
Native compression |
Getting started
This basic code snippet should get you ready for more. Remember that routed methods
(the ones that are called on an HTTP endpoint) must be defined with async def
, not def
!
from Aeros import WebServer
from Aeros.misc import jsonify
app = WebServer(__name__, host="0.0.0.0", port=80)
@app.route("/")
async def home():
return jsonify({"response": "ok"})
if __name__ == '__main__':
app.start("-w 2") # worker threads (for more arguments see hypercorn documentation)
Full Documentation
Using sync methods in async methods
If you need to execute a synchronous method in an HTTP request handler and need to wait
for its response, you should use sync_to_async
from asgiref.sync
. This method can also
be imported from Aeros.misc
:
from Aeros.misc import sync_to_async
import time
@sync_to_async
def sync_method():
time.sleep(2)
return "ok"
@app.route("/")
async def home():
status = sync_method()
return jsonify({"response": status})
Starting a server in a separate thread
Quart and Hypercorn don't allow server instances to be started from a non __main__
thread.
Aeros however does. This code shows how:
from Aeros import WebServer
from Aeros.threading import AdvancedThread
from threading import Thread
import time
app = WebServer(__name__, host="0.0.0.0", port=80, worker_threads=2)
...
if __name__ == '__main__':
t = AdvancedThread(target=app.run_server, daemon=True)
# OR
t = Thread(target=app.run_server, daemon=True)
t.start()
time.sleep(120)
t.stop() # only available in AdvancedThread, not in Thread
Headers
Adding custom global headers
You can define headers, which will be sent on every response, no matter the response type.
from Aeros import WebServer
app = WebServer(__name__, global_headers={"foo":"bar"})
...
Remove the server
header
The server
header can be removed on initialization:
from Aeros import WebServer
app = WebServer(__name__, include_server_header=False)
...
Caching
By default, WebServer()
has no cache configured. You can choose between
multiple cache types to start your server instance with:
Cache Type | Description |
---|---|
SimpleCache() |
Easy to set-up, not very stable with multiple worker threads. |
FilesystemCache() |
Stores every unique request in a separate file in a given directory. |
RedisCache() |
Stores cached objects on a given Redis server. |
Here, the most basic example
from Aeros import WebServer
from asyncio import sleep
from Aeros import SimpleCache
cache = SimpleCache(timeout=10, # Cache objects are deleted after this time [s]
threshold=10 # Only 10 objects are stored in cache
)
app = WebServer(__name__, host="0.0.0.0", port=80, worker_threads=4, cache=cache)
@app.route("/")
@app.route("/<path:path>")
@app.cache()
async def index(path=""):
print(path)
if path != "favicon.ico":
await sleep(5)
return "test"
if __name__ == '__main__':
app.run_server()
Compression
Aeros supports gzip compression, which is enabled by default (for all text-based files >500 bytes, with compression level 2). You can customize these compression settings by default
from Aeros import WebServer, Compress, AdvancedThread
import time
# For more information:
# https://github.com/colour-science/flask-compress
app = WebServer(__name__, host="0.0.0.0", port=80, worker_threads=2, )
app.config["COMPRESS_MIN_SIZE"] = 5 # size in bytes
app.config["COMPRESS_LEVEL"] = 2 # compresses to about 25% of original size
app.config["COMPRESS_MIMETYPES"] = [ # compresses all text-based things
'text/plain',
'text/html',
'text/css',
'text/scss',
'text/xml',
'application/json',
'application/javascript'
]
Compress(app)
@app.route("/")
async def home():
return "testing again..."
if __name__ == '__main__':
t = AdvancedThread(target=app.run_server, daemon=True)
t.start()
time.sleep(120)
t.stop() # only available in AdvancedThread, not in Thread
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file Aeros-1.0.3.tar.gz
.
File metadata
- Download URL: Aeros-1.0.3.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fa6231c9cee75fa93b3e05a783d17843b3cc69dff3f06457cf9df1d5440dabd |
|
MD5 | 4506d6f61b2e84eea3206e7bfe9ba008 |
|
BLAKE2b-256 | 4f6e0d8b66401b87e71c3c0184494a7c5c5a42478d701dfa25e51609d58c226a |
File details
Details for the file Aeros-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: Aeros-1.0.3-py3-none-any.whl
- Upload date:
- Size: 17.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 540e8cdbb7b98907c703ca769fae63501ee7189c2b6ab65a7113c97da070b6a1 |
|
MD5 | 4d4cd5faf2e3c2f70d17104654ae5e82 |
|
BLAKE2b-256 | e4fb4b80a9657bed7065b0b62b1d18fad3b26b6b2054750e493003353049db5e |