aiohttp

How i call async function without await?

≯℡__Kan透↙ 提交于 2020-03-14 07:46:54
问题 I have a controller action in aiohttp application. async def handler_message(request): try: content = await request.json() perform_message(x,y,z) except (RuntimeError): print("error in perform fb message") finally: return web.Response(text="Done") perform_message is async function. Now, when I call action I want that my action return as soon as possible and perform_message put in event loop. In this way, perform_message isn't executed 回答1: One way would be to use create_task function: import

一种在 python 中用异步协程实现的HTTP-API测试工具

南楼画角 提交于 2020-02-27 07:23:33
1 实现内容 通过包装 python 的内部模块 argparse , asyncio 和第三方库 aiohttp ,结合 python 3.5 之后的 async , await , 装饰器 等语法糖编写了一个 HTTP-API 测试工具,各 HTTP-API 以协程为单位异步并发请求。 对于主要心思花在 诸如 HTTP 结合 RESTful 接口设计开发或架构相关得广泛 的角色可以考虑用这个工具测试所编写的HTTP-API接口。如和是30个相同HTTP-API的请求的运行打个照面(异步并发将节约2s时间): ... 15 http://api.heclouds.com:80/ipc/video/boot_address 31.41ms 17 http://api.heclouds.com:80/ipc/video/boot_address 46.86ms 20 http://api.heclouds.com:80/ipc/video/boot_address 46.86ms sync request total time: 1939.13ms async request total time: 203.08ms time saved about 1736.05ms from sync to async ---------------------done-------------

Running URL Requests in Parallel with Flask

爱⌒轻易说出口 提交于 2020-02-07 03:08:48
问题 asyncio is still relatively new for me. I am starting with the basics - simple HTTP hello world - just making approximately 40 parallel GET requests and fetching the first 400 characters of the HTTP responses using Flask ("parallel" function is invoked by request). It is running on python 3.7. The Traceback is showing errors I don't understand. Which "Constructor parameter should be str" is this referring to? How should I proceed? This is the entire code of the app: import aiohttp import

AIOHTTP - Application.make_handler(…) is deprecated - Adding Multiprocessing

白昼怎懂夜的黑 提交于 2020-02-02 15:45:29
问题 I went down a journey of "How much performance can I squeeze out of a Python web-server?" This lead me to AIOHTTP and uvloop. Still, I could see that AIOHTTP wasn't using my CPU to its full potential. I set out to use multiprocessing with AIOHTTP. I learned that there's a Linux kernel feature that allows multiple processes to share the same TCP port. This lead me to develop the following code (Which works wonderfully): import asyncio import os import socket import time from aiohttp import web

AIOHTTP - Application.make_handler(…) is deprecated - Adding Multiprocessing

允我心安 提交于 2020-02-02 15:45:10
问题 I went down a journey of "How much performance can I squeeze out of a Python web-server?" This lead me to AIOHTTP and uvloop. Still, I could see that AIOHTTP wasn't using my CPU to its full potential. I set out to use multiprocessing with AIOHTTP. I learned that there's a Linux kernel feature that allows multiple processes to share the same TCP port. This lead me to develop the following code (Which works wonderfully): import asyncio import os import socket import time from aiohttp import web

Using Socket IO and aiohttp for data transfer between node JS and Python

…衆ロ難τιáo~ 提交于 2020-01-30 13:00:49
问题 My overall goal is to generate a stream of random numbers in a JavaScript file (which is run using node) and send them to a python script in asynchronous time intervals. Once the numbers are in python, the script will determine if the numbers are even, or not. If they are, the numbers are sent back to the JavaScript file. My main focus is to get the communication between JavaScript and Python. Once I begin the JavaScript file and python server, they will continue running until I stop them.

how to connect a discord bot through proxy

我们两清 提交于 2020-01-25 04:39:07
问题 I am trying to run a discord bot using discord.py and through a proxy. The discordpy doc on this is pretty scarce on the subject and not up to date with aiohttp implementation. discordpy doc basically says to use a ProxyConnector and pass it as an argument when the client is created. But in aiohttp, this way is deprecated and client.ClientSession().get is recommended instead. Problem is, client.ClientSession().get asks me to provide a URL. I also tried with ProxyConnector anyway, but it doesn

aiohttp slowness with threading

China☆狼群 提交于 2020-01-24 14:11:07
问题 I copied the code from How to run an aiohttp server in a thread?. It runs fine. So I am adding one second sleep. When I launch 10 requests at the same time. The average response time is 9 seconds. Why is that? Wouldn't all requests coming back in a little bit over 1 second? import asyncio import threading from aiohttp import web import time loop = asyncio.get_event_loop() def say_hello(request): time.sleep(1) return web.Response(text='Hello, world') app = web.Application(debug=True) app.add

aiohttp slowness with threading

假装没事ソ 提交于 2020-01-24 14:08:05
问题 I copied the code from How to run an aiohttp server in a thread?. It runs fine. So I am adding one second sleep. When I launch 10 requests at the same time. The average response time is 9 seconds. Why is that? Wouldn't all requests coming back in a little bit over 1 second? import asyncio import threading from aiohttp import web import time loop = asyncio.get_event_loop() def say_hello(request): time.sleep(1) return web.Response(text='Hello, world') app = web.Application(debug=True) app.add

Requests/aiohttp: closing response objects

a 夏天 提交于 2020-01-23 09:04:56
问题 I'm a bit confused about the need to .close() a response object in both requests and aiohttp . (Note that this is a separate instance method than session.close() --I'm talking about the response object itself.) Does Response ( requests ) or ClientResponse ( aiohttp ) ever need explicitly call .close() ? If not, what is the purpose of using the response itself as a context manager? ( async with session.request('GET', 'https://www.pastebin.com' ) below.) Why define the two dunder methods for