How to continue processing after returning a response on aiohttp Server

If you are creating a web server with aiohttp and want to run a slightly heavy process when receiving an HTTP request [^ job], if you run the process as it is, it will be slow to return an HTTP response, and as a timeout error on the client side It may be processed. Assuming that there is no need to return the processing result, what should I do if I want to return an HTTP response once to end HTTP communication and then perform a little heavy processing?

How to write_eof </ del>

** I found this method not good, so see the postscript. ** **

from asyncio import sleep
from aiohttp import web


async def handler(request):
    response = web.Response()
    await response.prepare(request)
    await response.write_eof()

    await sleep(1)  #Processing after returning a response


app = web.Application()
app.router.add_get('/', handler)

if __name__ == '__main__':
    web.run_app(app)
Just create a Response object and `prepare` and` write_eof`.

If nothing is specified when creating the Response object, the HTTP status code is 200.

[2020.06 postscript] It was useless

With the above method, if the connection is maintained for a certain period of time with keep-alive, it happens to work without problems, so I did not notice it, but basically it does not work well.

If you add Connection: close to the response header and disable keep-alive, you can see that the subsequent asynchronous processing is not executed at the time ofwrite_eof ().

Apparently, when the connection with the client is broken, the processing of the request handler is stopped there. (It was hard to find out because there was no error message)

Reference: https://github.com/aio-libs/aiohttp/issues/4773

As a solution, it seems better to use aiojobs.

from asyncio import sleep
from aiohttp import web
import aiojobs.aiohttp as aiojobs


async def job():
    """The process you want to execute after returning a response"""
    await sleep(1)


async def handler(request):
    await aiojobs.spawn(request, job())
    return web.Response(text='ok')


app = web.Application()
app.router.add_get('/', handler)

aiojobs.setup(app)

if __name__ == '__main__':
    web.run_app(app)

Recommended Posts

How to continue processing after returning a response on aiohttp Server
How to run Django on IIS on a Windows server
A note on how to check the connection to the license server port
How to test on a Django-authenticated page
Verification of how to periodically execute a script on a Linux server on Windows
How to live a decent life on 2017 Windows
How to set up a local development server
How to deploy a Django application on Alibaba Cloud
How to install Linux on a 32bit UEFI PC
A memorandum on how to use keras.preprocessing.image in Keras
How to build a Django (python) environment on docker
How to create a simple TCP server / client script
How to build a Python environment on amazon linux 2
[Linux] How to install a package on a server that does not have a network environment (standalone)
How to use Fujifilm X-T3 as a webcam on Ubuntu 20.04
A memo on how to easily prepare a Linux exercise environment
How to run a trained transformer model locally on CloudTPU
How to build a new python virtual environment on Ubuntu
SSH connection to a private server using a bastion server on EC2
How to make a multiplayer online action game on Slack
How to specify a public directory Python simple HTTP server
How to mount a Windows 10 directory on Ubuntu-Server 20.04 on VMware Workstation 15
A note on how to load a virtual environment in PyCharm
How to write a test for processing that uses BigQuery
[Introduction to AWS] A memorandum of building a web server on AWS
How to register a package on PyPI (as of September 2017)
How to call a function
How to register on pypi
How to hack a terminal
How to set cron for regular Python scraping on Sakura server.
I tried to create a server environment that runs on Windows 10
Overview of how to create a server socket and how to establish a client socket
How easy is it to synthesize a drug on the market?
How to customize U-Boot with OSD335X on a custom board (memo)
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to make a .dylib library from a .a library on OSX (El Capitan)
How to deploy a Django app on heroku in just 5 minutes
How to deploy a web application on Alibaba Cloud as a freelancer
How to make a Japanese-English translation
How to write a Python class
How to put a symbolic link
How to install mysql-connector-python on mac
Notes on how to use pywinauto
How to make a slack bot
How to create a Conda package
How to install graph-tool on macOS
Run a Linux server on GCP
How to install VMware-Tools on Linux
How to install pycrypto on Windows
How to make a crawler --Advanced
How to deploy django-compressor on Windows
Notes on how to use featuretools
How to create a virtual bridge
How to install OpenCV on Mac
How to run matplotlib on heroku
How to install PyPy on CentOS
How to use homebrew on Debian
Misunderstanding on how to connect cnn
How to install TensorFlow on CentOS 7
How to make a deadman's switch
How to create a Dockerfile (basic)