Questions tagged [aiohttp]

Use this tag for questions about AIOHTTP – a client/server framework for asyncio Python.

AIOHTTP is an asynchronous asyncio Python web framework for both client and server sides. It supports HTTP, WebSockets, and also MiddleWare and Signals mechanics for the server side.

Useful links:

Related tags:

1628 questions
199
votes
7 answers

How could I use requests in asyncio?

I want to do parallel http request tasks in asyncio, but I find that python-requests would block the event loop of asyncio. I've found aiohttp but it couldn't provide the service of http request using a http proxy. So I want to know if there's a way…
flyer
  • 9,280
  • 11
  • 46
  • 62
114
votes
8 answers

RuntimeError: There is no current event loop in thread in async + apscheduler

I have a async function and need to run in with apscheduller every N minutes. There is a python code below URL_LIST = ['', '', '', ] def demo_async(urls): """Fetch list of web pages…
Valera Shutylev
  • 1,145
  • 2
  • 7
  • 6
103
votes
3 answers

Learning asyncio: "coroutine was never awaited" warning error

I am trying to learn to use asyncio in Python to optimize scripts. My example returns a coroutine was never awaited warning, can you help to understand and find how to solve it? import time import datetime import random import asyncio import…
Anthony Hervy
  • 1,201
  • 2
  • 7
  • 14
92
votes
6 answers

How can I wrap a synchronous function in an async coroutine?

I'm using aiohttp to build an API server that sends TCP requests off to a seperate server. The module that sends the TCP requests is synchronous and a black box for my purposes. So my problem is that these requests are blocking the entire API. I…
Zac Delventhal
  • 3,543
  • 3
  • 20
  • 26
76
votes
3 answers

How to combine python asyncio with threads?

I have successfully built a RESTful microservice with Python asyncio and aiohttp that listens to a POST event to collect realtime events from various feeders. It then builds an in-memory structure to cache the last 24h of events in a nested…
fxstein
  • 1,133
  • 1
  • 9
  • 12
63
votes
5 answers

How can I call an async function without await?

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") …
Pasalino
  • 992
  • 1
  • 9
  • 15
49
votes
4 answers

aiohttp: set maximum number of requests per second

How can I set maximum number of requests per second (limit them) in client side using aiohttp?
v18o
  • 1,237
  • 2
  • 15
  • 25
36
votes
1 answer

RuntimeWarning: Enable tracemalloc to get the object allocation traceback with asyncio.sleep

Trying to use a semaphore to control asynchronous requests to control the requests to my target host but I am getting the following error which I have assume means that my asycio.sleep() is not actually sleeping. How can I fix this? I want to add a…
Liondancer
  • 15,721
  • 51
  • 149
  • 255
35
votes
5 answers

aiohttp: rate limiting parallel requests

APIs often have rate limits that users have to follow. As an example let's take 50 requests/second. Sequential requests take 0.5-1 second and thus are too slow to come close to that limit. Parallel requests with aiohttp, however, exceed the rate…
Boffin
  • 1,197
  • 2
  • 11
  • 18
33
votes
2 answers

Python - Attempt to decode JSON with unexpected mimetype:

I recently swapped over from requests to aiohttp because I couldn't use it in asyncio loops. The swap went perfectly and everything goes well except for one thing. My console is full of Attempt to decode JSON with unexpected mimetype: and Attempt…
pfych
  • 850
  • 1
  • 13
  • 32
33
votes
2 answers

"async with" in Python 3.4

The Getting Started docs for aiohttp give the following client example: import asyncio import aiohttp async def fetch_page(session, url): with aiohttp.Timeout(10): async with session.get(url) as response: assert…
Imran
  • 12,950
  • 8
  • 64
  • 79
32
votes
4 answers

How to mock aiohttp.client.ClientSession.get async context manager

I have some troubles with mocking aiohttp.client.ClientSession.get context manager. I found some articles and here is one example that seems was working: article 1 So my code that I want to test: async_app.py import random from aiohttp.client import…
32
votes
3 answers

aiohttp: how-to retrieve the data (body) in aiohttp server from requests.get

Could you please advise on the following? On the localhost:8900 there is aiohttp server running When I do a request like (using the python2 module requests) from python requests.get("http://127.0.01:8900/api/bgp/show-route", …
nskalis
  • 2,232
  • 8
  • 30
  • 49
31
votes
1 answer

aiohttp web.response body as json

I have HTTP server on aiohttp with python-3.6. How can I return web.Response() through JSON (from a dict)? async def api_server(request): res = {"q": "qqq", "a": "aaa"} return web.Response(res) # <-- as JSON
morfair
  • 518
  • 1
  • 6
  • 17
28
votes
1 answer

Parallel asynchronous IO in Python's coroutines

Simple example: I need to make two unrelated HTTP requests in parallel. What's the simplest way to do that? I expect it to be like that: async def do_the_job(): with aiohttp.ClientSession() as session: coro_1 =…
George Sovetov
  • 4,942
  • 5
  • 36
  • 57
1
2 3
99 100