How to make wsgiref asynchronous?

Below I have described a program that aims to serve asynchronous http requests using the wsgiref module with a simple implementation to make it ( asgiref ) :slight_smile:

# Native Module : asyncio — Asynchronous I/O -> https://docs.python.org/3/library/asyncio.html#module-asyncio
import asyncio, inspect, threading, time
# Native Module : wsgiref.simple_server -> ( https://docs.python.org/3/library/wsgiref.html#module-wsgiref )
from wsgiref.simple_server import make_server


# Syntax Objective -> convert function to coroutine and be able to run concurrently ( asynchronous )
async def asgiref(environ, start_response, loop):

    await asyncio.sleep(0.01)

    status = '200 OK'  # HTTP Status
    headers = [('Content-type', 'text/plain; charset=utf-8')]  # HTTP Headers
    start_response(status, headers)

    # The returned object is going to be printed
    return [str(f'Thread Main: {threading.active_count()}, Thread Id: {threading.get_ident()}, Thread Time: Processed in {time.thread_time()} (seconds), Event Loop: {loop}.').encode('utf-8')]

def wsginter(environ, start_response):

    # Syntax Objective -> capture the current event loop, but it doesn't exist ?
    # asyncio will create a new event loop and set it to current.
    loop = asyncio.get_event_loop()
    # Syntax Objective -> run until it terminate the loop and trigger the coroutine :
    return loop.run_until_complete(asgiref(environ, start_response, loop))

# Syntax Objective -> start the test server and trigger the function intermediary to wsgiref
with make_server('', 8000, wsginter) as httpd:

    print(f'Serving on port 8000...')
    # Serve until process is killed
    httpd.serve_forever()

( ☉☉ ) → The code above works, but how can I know if it’s really asynchronous (would I be able to capture the secondary thread and know if it’s running?), because on the main thread I got it, through the function ( get_ident() ) of the class ( threading ) , as above. And what would be the reason for the error ( Deprecation Warning: There is no current event loop, as it is understood that the loop does not exist, but is captured through the loop object ) ?

You’re looking for the ASGI spec: ASGI Documentation — ASGI 3.0 documentation

Frameworks such as Quart (Flask), Starlette, Sanic, Falcon, FastAPI, and others allow creating ASGI applications. ASGI servers include Hypercorn and Uvicorn.

Flask and Django both implement translation allowing async views in WSGI, but without some of the capabilities of ASGI.

I’m working on the development of a web framework different from the ones that exist and that’s why I see the need to insert asynchronous http requests