Files
fastwsgi/fastwsgi.py
Oleg S 882fe7972f Improves and fixes (#19)
* Add support log subsystem

* Replace logger calls to LOGi (LL_INFO)

* Add new global object: server_t g_srv

* Make current_header as static char array

* Make global struct g_cv which contain static Py strings

* Add new const values to global struct g_cv

* Add loglevel option to fastwsgi.py

* Fix bug then response body contain 0x00 symbol

* Add new header file: common.h

* Replace response buffer type to xbuf_t (new expanded buffer)

* Cleanup function build_response (remove waste alloc mem)

* Add extended logging into server.c

* Update submodule llhttp to 8.1.0 release

* Add functions build_response_ex and send_fatal

* Fix send error message then use Keep-Alive connection

* Fix call uv__socket_sockopt (first arg incorrect)
2022-10-17 08:14:46 +02:00

90 lines
2.7 KiB
Python

import os
import sys
import signal
import importlib
import click
import _fastwsgi
from pkg_resources import get_distribution
NUM_WORKERS = 4
HOST = "0.0.0.0"
PORT = 5000
BACKLOG = 1024
LOGLEVEL = 0
def run_multi_process_server(app):
workers = []
for _ in range(NUM_WORKERS):
pid = os.fork()
if pid > 0:
workers.append(pid)
print(f"Worker process added with PID: {pid}")
else:
try:
_fastwsgi.run_server(app, HOST, PORT, BACKLOG, 0)
except KeyboardInterrupt:
exit()
try:
for _ in range(NUM_WORKERS):
os.wait()
except KeyboardInterrupt:
print("\nStopping all workers")
for worker in workers:
os.kill(worker, signal.SIGINT)
def import_from_string(import_str):
module_str, _, attrs_str = import_str.partition(":")
if not module_str or not attrs_str:
raise ImportError("Import string should be in the format <module>:<attribute>")
try:
module = importlib.import_module(module_str)
for attr_str in attrs_str.split("."):
module = getattr(module, attr_str)
except AttributeError:
raise ImportError(f'Attribute "{attrs_str}" not found in module "{module_str}"')
return module
def print_server_details(host, port):
print(f"\n==== FastWSGI ==== ")
print(f"Host: {host}\nPort: {port}")
print("==================\n")
@click.command()
@click.version_option(version=get_distribution("fastwsgi").version, message="%(version)s")
@click.option("--host", help="Host the socket is bound to.", type=str, default=HOST, show_default=True)
@click.option("-p", "--port", help="Port the socket is bound to.", type=int, default=PORT, show_default=True)
@click.option("-l", "--logging", help="Logging level.", type=int, default=LOGLEVEL, show_default=True)
@click.argument(
"wsgi_app_import_string",
type=str,
required=True,
)
def run_from_cli(host, port, wsgi_app_import_string, logging):
"""
Run FastWSGI server from CLI
"""
try:
wsgi_app = import_from_string(wsgi_app_import_string)
except ImportError as e:
print(f"Error importing WSGI app: {e}")
sys.exit(1)
print_server_details(host, port)
print(f"Server listening at http://{host}:{port}")
_fastwsgi.run_server(wsgi_app, host, port, BACKLOG, logging)
def run(wsgi_app, host=HOST, port=PORT, backlog=1024, loglevel=LOGLEVEL):
print_server_details(host, port)
print(f"Server listening at http://{host}:{port}")
print(f"Running on PID:", os.getpid())
_fastwsgi.run_server(wsgi_app, host, port, backlog, loglevel)
# run_multi_process_server(wsgi_app)