Python Forum
Wsgiref with asyncio ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wsgiref with asyncio ?
#5
(Oct-28-2022, 02:59 PM)wavic Wrote: Maybe this could give you some directions.

Note that the syntax for making a coroutine is async def/await now. That one there was the old way. Using a decorator...

You are awaiting instead of yield from

I managed to define a simple and very logical implementation, but I come across the following error:

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


# Function -> define coroutine, to run the function asynchronous
async def maincoro(environ, start_response):

    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('Test of requisition http ?').encode('utf-8')]

# Function -> prepare a loop to work with asynchronous events ( request list ) :
loop = get_event_loop()
# Function -> run the loop until finished :
coro = loop.run_until_complete(maincoro(environ, start_response))

with make_server('', 8000, coro) as httpd:
    print('Serving on port 8000...')
    # Serve until process is killed
    httpd.serve_forever()
Error :
(wse) joe@debian:~/PycharmProjects/quark$ python trycode.py
/home/joe/PycharmProjects/quark/trycode.py:31: DeprecationWarning: There is no current event loop
  loop = get_event_loop()
Traceback (most recent call last):
  File "/home/joe/PycharmProjects/quark/trycode.py", line 33, in <module>
    coro = loop.run_until_complete(maincoro(environ, start_response))
NameError: name 'environ' is not defined
Reply


Messages In This Thread
Wsgiref with asyncio ? - by JohnnyCoffee - Oct-27-2022, 06:59 PM
RE: Wsgiref with asyncio ? - by wavic - Oct-28-2022, 11:17 AM
RE: Wsgiref with asyncio ? - by JohnnyCoffee - Oct-28-2022, 02:12 PM
RE: Wsgiref with asyncio ? - by wavic - Oct-28-2022, 02:59 PM
RE: Wsgiref with asyncio ? - by JohnnyCoffee - Oct-30-2022, 10:36 PM
RE: Wsgiref with asyncio ? - by wavic - Oct-31-2022, 02:50 AM
RE: Wsgiref with asyncio ? - by JohnnyCoffee - Oct-31-2022, 03:42 PM
RE: Wsgiref with asyncio ? - by wavic - Oct-31-2022, 03:47 PM
RE: Wsgiref with asyncio ? - by JohnnyCoffee - Nov-01-2022, 08:35 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020