C
C
cfmm2018-05-24 18:12:54
Python
cfmm, 2018-05-24 18:12:54

aio http library. Is it possible to parallelize query processing?

Made a simple web server using aiohttp 3.3 and Python 3.6.5
As planned, the request handler should be asynchronous and handle multiple requests in parallel.
To check, I made two requests to the server at the same time and realized that in fact the parallel processing did not work (see the result below)
What am I doing wrong?

from aiohttp import web
import asyncio

async def handler(request):
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    await asyncio.sleep(5)
    return web.json_response({"ok": 1})

app = web.Application()
app.add_routes([web.view('/', handler)])
web.run_app(app, host='0.0.0.0', port=8080)

Messages in the terminal with an interval of 5 seconds:
2018-05-24 17:49:58
2018-05-24 17:50:03

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2018-05-24
@cfmm

Surely you open two tabs in the same browser and send requests from them. But neither Google Chrome nor Mozila Firefox will send a second request to the same address until they receive a response to the first one. Even if you run two windows. But IE11 sends two requests at the same time to the same address from the tabs without any problems. Try running two different browsers, or even better, use curl for tests.

Z
Zanak, 2018-05-27
@Zanak

Parallelism is not asynchrony.
Parallel execution of tasks means that they are executed simultaneously, well, almost simultaneously.
Asynchronous execution is execution in which the processor can work on another task while the current one is waiting for the completion of a long operation, such as a network transfer, or disk work, for example.
aiohttp asynchronous framework, or am I missing something?

C
cfmm, 2018-05-28
@cfmm

Yes, I misunderstood. Concurrency is not asynchrony
aiohttp - asynchronous framework

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question