aiohttp

Fetching multiple urls with aiohttp in python

假装没事ソ 提交于 2019-12-08 00:36:57
问题 In a previous question, a user suggested the following approach for fetching multiple urls (API calls) with aiohttp : import asyncio import aiohttp url_list = ['https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530396000&before=1530436000', 'https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530436000&before=1530476000'] async def fetch(session, url): async with session.get(url) as response: return await response.json()['data'] async def fetch_all

nested “async with” using aiohttp

我的梦境 提交于 2019-12-07 18:09:41
问题 I would like to create a scheduler class that uses aiohttp to make API calls. I tried this: import asyncio import aiohttp class MySession: def __init__(self): self.session = None async def __aenter__(self): async with aiohttp.ClientSession() as session: self.session = session return self async def __aexit__(self, exc_type, exc_val, exc_tb): if self.session: await self.session.close() async def method1(): async with MySession() as s: async with s.session.get("https://www.google.com") as resp:

asynchronous aiohttp requests fails, but synchronous requests succeed

限于喜欢 提交于 2019-12-07 02:10:39
问题 With the following code I get Cannot connect to host ...:443 ssl:True when I use the asynchronous aiohttp . When I use synchronous requests , it succeeds. The whitehouse.gov links fail, but the google.com succeeds for both async and sync cases. What is going wrong? This is with python 3.4.2 on FreeBSD8, aiohttp 0.14.4, requests 2.5.3 import asyncio import aiohttp import requests urls = [ 'http://www.whitehouse.gov/cea/', 'http://www.whitehouse.gov/omb', 'http://www.google.com'] def test_sync(

Fetching multiple urls with aiohttp in python

ぐ巨炮叔叔 提交于 2019-12-06 13:31:07
In a previous question , a user suggested the following approach for fetching multiple urls (API calls) with aiohttp : import asyncio import aiohttp url_list = ['https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530396000&before=1530436000', 'https://api.pushshift.io/reddit/search/comment/?q=Nestle&size=30&after=1530436000&before=1530476000'] async def fetch(session, url): async with session.get(url) as response: return await response.json()['data'] async def fetch_all(session, urls, loop): results = await asyncio.gather(*[loop.create_task(fetch(session, url)) for url in

Python asyncio/aiohttp: What are the requirements regarding BaseProtocol.connection_lost()?

二次信任 提交于 2019-12-06 10:21:16
The python documentation for connection_lost states: connection_made() and connection_lost() are called exactly once per successful connection. Further down there's also the following state machine: start -> connection_made() [-> data_received() *] [-> eof_received() ?] -> connection_lost() -> end Also, the documentation for BaseTransport.close() states: After all buffered data is flushed, the protocol’s connection_lost() method will be called with None as its argument. and the documentation for WriteTransport.abort() states: The protocol’s connection_lost() method will eventually be called

Asyncio and rabbitmq (asynqp): how to consume from multiple queues concurrently

淺唱寂寞╮ 提交于 2019-12-06 06:08:45
I'm trying to consume multiple queues concurrently using python, asyncio and asynqp . I don't understand why my asyncio.sleep() function call does not have any effect. The code doesn't pause there. To be fair, I actually don't understand in which context the callback is executed, and whether I can yield control bavck to the event loop at all (so that the asyncio.sleep() call would make sense). What If I had to use a aiohttp.ClientSession.get() function call in my process_msg callback function? I'm not able to do it since it's not a coroutine. There has to be a way which is beyond my current

Python asyncio/aiohttp: ValueError: too many file descriptors in select() on Windows

瘦欲@ 提交于 2019-12-06 04:48:31
问题 Hello everyone, I'm having trouble trying to understand asyncio and aiohttp and making both work together properly. Not only I don't properly understand what I'm doing, at this point I've run into a problem that I have no idea how to solve. I'm using Windows 10 64 bits, latest update. The following code returns me a list of pages that do not contain html in the Content-Type in the header using asyncio. import asyncio import aiohttp MAXitems = 30 async def getHeaders(url, session, sema): async

Requests/aiohttp: closing response objects

醉酒当歌 提交于 2019-12-05 17:30:52
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 this if it gets closed implicitly as shown below? Some simple tests (below) seem to imply that responses

aiohttp Error Rate Increases with Number of Connections

半世苍凉 提交于 2019-12-05 12:15:59
I am trying to get the status code from millions of different sites, I am using asyncio and aiohttp, I run the below code with a different number of connections (yet same timeout on the request) but get very different results specifically much higher number of the following exception. 'concurrent.futures._base.TimeoutError' The code import pandas as pd import asyncio import aiohttp out = [] CONNECTIONS = 1000 TIMEOUT = 10 async def fetch(url, session, loop): try: async with session.get(url,timeout=TIMEOUT) as response: res = response.status out.append(res) return res except Exception as e:

asynchronous aiohttp requests fails, but synchronous requests succeed

孤街浪徒 提交于 2019-12-05 08:17:36
With the following code I get Cannot connect to host ...:443 ssl:True when I use the asynchronous aiohttp . When I use synchronous requests , it succeeds. The whitehouse.gov links fail, but the google.com succeeds for both async and sync cases. What is going wrong? This is with python 3.4.2 on FreeBSD8, aiohttp 0.14.4, requests 2.5.3 import asyncio import aiohttp import requests urls = [ 'http://www.whitehouse.gov/cea/', 'http://www.whitehouse.gov/omb', 'http://www.google.com'] def test_sync(): for url in urls: r = requests.get(url) print(r.status_code) def test_async(): for url in urls: try: