Monday, 28 January 2019

Python asyncio skip processing untill function return

I'm still very confused about how asyncio works, so I was trying to set a simple example but couldn't achieve it.

The following example is a web server (Quart) that receives a request to generate a large PDF, the server then returns a response before start processing the PDF, then starts processing it and will send the download link to an email later.

from quart import Quart
import asyncio
import time

app = Quart(__name__)

@app.route('/')
async def pdf():
    t1 = time.time()
    await generatePdf()
    return 'Time to execute : {} seconds'.format(time.time() - t1)

async def generatePdf():
    await asyncio.sleep(5)
    #sync generatepdf
    #send pdf link to email

app.run()

How would I go about this? in the above example I don't want the 5 seconds to be waited before the return.

I'm not even sure if asyncio is what I need.

And I'm afraid that blocking the server app after the response has returned is not a thing that should be done, but not sure either.

Also the pdf library is synchronous, but I guess that's a problem for another day...



from Python asyncio skip processing untill function return

No comments:

Post a Comment