Showing posts with label python3. Show all posts
Showing posts with label python3. Show all posts

Friday, 31 July 2026

python uvicorn vs wsgi

 Uvicorn is a specific, high-performance web server program, whereas WSGI is an abstract specification defining how Python applications talk to web servers. [1, 2]

To compare them properly, you must look at Uvicorn (an ASGI server implementation) versus WSGI servers (like Gunicorn or uWSGI). The core difference lies in how they handle concurrency: WSGI servers handle requests synchronously using separate processes or threads, while Uvicorn handles them asynchronously via a single-thread event loop. [1, 2, 3, 4, 5]

Core Architecture Comparison
FeatureUvicorn (ASGI Server)WSGI Servers (e.g., Gunicorn)
NatureImplementation (Software)Specification (Standard)
Execution ModelAsynchronous (async/await)Synchronous (Blocking)
ConcurrencySingle-threaded event loop per workerProcess or thread per connection
Supported ProtocolsHTTP/1.1, WebSockets, HTTP/2HTTP/1.1 only
Best Framework FitFastAPI, Starlette, Async DjangoFlask, traditional Django

Key Strengths of Uvicorn
  • High Concurrency: Uses an event loop powered by uvloop to manage thousands of open connections at once.
  • Real-Time Communication: Supports WebSockets natively, removing the need for external event routers.
  • Low Memory Footprint: Handles high traffic using fewer system resources than standard multi-process setups. [1, 2, 3, 4, 5]

Key Strengths of WSGI Servers
  • Stable and Mature: Battle-tested for over a decade in major production environments.
  • Predictable Process Isolation: If a request crashes a worker process, other processes keep running.
  • No Async Overhead: Faster for purely CPU-bound tasks or apps without any asynchronous code. [1, 2, 3, 4, 5]

The Production Compromise
You often do not have to choose between them. In cloud production environments, developers frequently deploy Gunicorn as a process manager to handle master routing and process crashes, while instructing it to use Uvicorn workers (UvicornWorker) to execute the actual async Python code. [1, 2, 3, 4, 5]
Detailed benchmarking and deployment setups can be reviewed via resources like the DeployHQ Server Guide or the Leapcell Server Overview. [1, 2]

Monday, 14 July 2025

Python mod_WSGI spawn more process

 https://flask.palletsprojects.com/en/stable/deploying/mod_wsgi/

mod_wsgi is a WSGI server integrated with the Apache httpd server. The modern mod_wsgi-express command makes it easy to configure and start the server without needing to write Apache httpd configuration.


$ mod_wsgi-express start-server wsgi.py --processes 4

Tuesday, 17 December 2024

pytho3, python3-saml, xml upgrade library version mismatch 2024

 https://github.com/xmlsec/python-xmlsec/issues/320

!!!!!!!!!!!!!!!!!!!!! be wary when doing docker-compose build --no-cache

“lxml & xmlsec libxml2 library version mismatch” error under uWSGI

It seems like lxml@5.2.1 uses libxml2@2.12.6. However, libxml2@2.12.7 has been released, and python-xmlsec@1.3.14 breaks because of this libxml2 version mismatch (?)

Fix:
in requirements.txt
lxml==4.9.3
xmlsec==1.3.13
python3-saml==1.11.0

Tuesday, 1 October 2024

regex 101, python3 recursively replace single quote inside of double quote

 import re


# Input string

input_str = [{'a': 'b', 'c': "ddd'sss", 'x': "text'with'quotes"}]

input_str = str(input_str)


# Regex pattern to find double quotes and then identify single quotes inside

pattern = r'("([^"]*?)\'([^"]*?)")'


# Replace single quotes with <test> inside double quotes using a lambda function

fixed_output_str = re.sub(pattern, lambda m: m.group(1).replace("'", "<test>"), input_str)


# Displaying the result

print("Output string with single quotes replaced:", fixed_output_str)

python3 sandbox

 https://trinket.io/embed/python3



Wednesday, 18 September 2024

Python change response content to JSON


https://stackoverflow.com/questions/16877422/whats-the-best-way-to-parse-a-json-response-from-the-requests-library

by default python 

response object saves actual resposne in "data"

https://www.geeksforgeeks.org/response-json-python-requests/


you can use response.json() to get the "data" list / object

to get everything:

 import json

import requests

response = requests.get(...)
json_data = json.loads(response.text)

// conver list to json string
json.dumps()

// convert json string to list
json.loads