zappa / Zappa

Serverless Python
https://zappa.ws/zappa
MIT License
3.25k stars 363 forks source link

Django async support #1131

Closed undiplomatic closed 5 months ago

undiplomatic commented 2 years ago

Context

Django has introduced native async support. It doesn't appear to be supported by Zappa at this time. Is this something that should be supported moving forwards?

Expected Behavior

views.py

# Helper function
async def lets_sleep():
    print('Starting lets_sleep()')
    await asyncio.sleep(5)
    client = httpx.Client()
    response_p = client.get("https://httpbin.org")
    print(response_p)
    print('Ending lets_sleep()')
    return response_p

# ASync view
async def async1(request):
    print('Starting async0 view')
    loop = asyncio.get_event_loop()
    loop.create_task(lets_sleep())
    print('Ending async0 view')
    return HttpResponse("async1")

Expect output:

Starting async0 view
Ending async0 view
Starting lets_sleep()
<Response [200 OK]>
Ending lets_sleep()

Actual Behavior

Starting async0 view
Ending async0 view
Starting lets_sleep()

Possible Fix

Steps to Reproduce

  1. Add helper function
  2. Add async view
  3. Configure urls.py
  4. Call async view from browser

Your Environment

monkut commented 1 year ago

Lambda is an ephemeral service that spins up an instance to handle each request. Async assumes that you have a long lived process that continuously handles multiple incoming requests, and implemented in order to share process power more effectively.

So async support doesn't seem to make a lot of sense when using lambda. (However, now that docker images are supported it would make sense there)

It needs investigation, but I wouldn't expect any improvement through the use of async and lambda. In fact, with the additional overhead, I would expect performance to degrade when compared to a non-async version.

(I am personally interested in FastAPI, but haven't used it since it seems to require async. BTW - I think django-ninja is similar, but not as light as FastAPI)

github-actions[bot] commented 5 months ago

Hi there! Unfortunately, this Issue has not seen any activity for at least 90 days. If the Issue is still relevant to the latest version of Zappa, please comment within the next 10 days if you wish to keep it open. Otherwise, it will be automatically closed.

github-actions[bot] commented 5 months ago

Hi there! Unfortunately, this Issue was automatically closed as it had not seen any activity in at least 100 days. If the Issue is still relevant to the latest version of Zappa, please open a new Issue.