Skip to content

Instantly share code, notes, and snippets.

View asvetlov's full-sized avatar

Andrew Svetlov asvetlov

  • constructor.org
  • Gijon
View GitHub Profile
import asyncio
import aiohttp
async def f(sem, client, url):
async with sem:
with async_timeout.timeout(5):
async with client.get(url) as resp:
print(resp.status)
print(await resp.text()) # resp.context.read(1024)
async def f(url):
txt = await get_url(url)
return txt
task = ensure_future(f())
await task
tasks = [t]
for t in tasks:
from aiohttp import web, web2, web_reqrep2
async def handle(request):
name = request.match_info.get('name', "Anonymous")
text = "Hello, " + name
# push promise
# add Link header in case of HTTP 1.1
# start sending response after push request
async def fetch(session, url):
async with session.get(url) as resp:
if resp.headers['Content-Type'] != 'plain/text':
resp.close()
else:
text = await resp.text()
# -*- coding: utf-8 -*-
import asyncio
import aioredis
from aiohttp import web
import json
@asyncio.coroutine
def ulist(request):
data = yield from request.json()
Elpy Error
The backend encountered an unexpected error. This indicates a bug in
Elpy. Please open a bug report with the data below in the Elpy bug
tracker:
https://github.com/jorgenschaefer/elpy/issues/new
```
Error Message
@asvetlov
asvetlov / gist:227af16ae9eb38db7f22
Created January 7, 2016 00:12
aiohttp memleaking test
import itertools
from aiohttp import CIMultiDict
import tracemalloc
def leaking(headers):
headers = ''.join(itertools.chain(
('status line',),
*((k, ': ', v, '\r\n') for k, v in headers.items())))
import asyncio
import warnings
class Timeout:
def __init__(self, timeout, *, raise_error=False, loop=None):
self._timeout = timeout
if loop is None:
loop = asyncio.get_event_loop()
self._loop = loop
@asvetlov
asvetlov / gist:218a1d7d05a540d70a21
Last active August 29, 2015 14:26
aiohttp client with cookies
import aiohttp
import asyncio
class MyClass:
def __init__(self):
self.data = {'username': 'me', 'password': 'pw'}
self.login_url = 'http://example.com/login'
self.client = aiohttp.ClientSession()
def f():
future = asyncio.Future()
future.set_result(5)
return future # yield from f() returns 5.
def g():
future = asyncio.Future()
asyncio.get_event_loop.call_later(1, future.set_result, 5)
return future # yield from g() also returns 5 but 1 second later