-
-
Save asvetlov/6e589b6e81b1fcdb7f11393f9d0a4973 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 main(): | |
sem = asyncio.Semaphore(100) | |
async with aiohttp.ClientSession() as client: | |
jobs = [f(sem, client, url) for i in urls] # 10000 | |
await asyncio.gather(*jobs) | |
# requests | |
resp = requests.get(url) | |
resp.text | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(f()) | |
loop.close() | |
class A: | |
def __init__(self): | |
self.client = aiohttp.ClientSession() | |
async def close(self): | |
await self.client.close() | |
async def fetch(self): | |
await self.client.get(...) | |
# settings.py | |
client = aiohttp.ClientSession() | |
db = await aiopg.create_engine(user=, password=) | |
async def test_g(): | |
a = A() | |
await a.client.get(...) | |
async def f(q, client): | |
while True: | |
try: | |
url = await q.get() | |
with async_timeout.timeout(5): | |
async with client.get(url) as resp: | |
print(resp.status) | |
print(await resp.text()) # resp.context.read(1024) | |
except | |
async def main(): | |
q = asyncio.Queue() | |
for u in urls: | |
await q.put(u) | |
async with aiohttp.ClientSession() as client: | |
jobs = [ensure_future(f(q, client)) for i in range(50)] | |
while True: | |
await sleep(1) | |
if q.empty(): | |
for j in jobs: | |
j.cancel() | |
##################################################### | |
from aiohttp import web | |
async def handler(request): | |
request.rel_url.query['user'] # GET /?user=John | |
return web.Response(text='OK') | |
async def handler(request): | |
request.match_info['name'] # GET /John/id | |
return web.Response(text='OK') | |
class A(web.View): | |
def __init__(self, request): | |
super().__init__(request) | |
async def get(self): | |
self.request | |
return web.Response() | |
class Users: | |
def __init__(self, db): | |
self.db = db | |
async def create(self, request): | |
self.lang = | |
async def get(self, request): | |
pass | |
async def delete(self, request): | |
pass | |
app = web.Application() | |
db = await create_db() | |
users = Users(db) | |
app.router.add_post('/users', users.create) | |
app.router.add_delete('/users/{id}', users.delete) | |
app.router.add_get('/users/{id}', users.get) | |
app.router.add_get('/{z:.*}', handler) | |
app.router.add_post('/post', handler2) | |
app.router.add_get('/{name:\d+}/id', handler3) | |
app.router.add_get('/{name2:\S+}/id', handler3) | |
app.router.add_route('GET', '/pewrwefsdf', A) | |
app.router.add_route('*', '/pewrwefsdf', A) | |
web.run_app(app) | |
$ pip install trafaret | |
$ pip install trafaret_config | |
import trafaret as t | |
import trafaret_config | |
CONFIG = t.Dict(server=t.Dict(port=t.Int), | |
db=t.Dict(db=t.String(50), | |
user=t.String, | |
passsword=t.String) | |
) | |
server: | |
port: 8080 | |
db: | |
db: dbname | |
user: user | |
password: password | |
############################# | |
app = web.Application() | |
# app['db'] = await create_db_connection() | |
app['config'] = read_config() | |
app.router.add_get() | |
async def on_init(app): | |
cfg = app['config'] | |
db = await create_db_coonection(config['db']) | |
app['db'] = db | |
app.on_startup += on_init | |
async def on_cleanup(app): | |
await app['db'].close() | |
app.on_cleanup += on_cleanup | |
async def handler(request): | |
request.app['db'] | |
return web.Response(text='OK') | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
loop = asyncio.get_event_loop() | |
class Proto(asyncio.Protocol): | |
def __init__(self, config): | |
self.transp = None | |
self.config = config | |
self.is_hdr = False | |
self.is_body = False | |
self.hdr = None | |
def connection_made(self, transp): | |
self.transp = transp | |
def _on_timeout(self): | |
self.transp.close() | |
def data_received(self, data): | |
if not self.hdr: | |
self.is_hdr = True | |
hdr = parse_hdr(data) | |
self._fut.set_result(hdr) | |
self._timeout = self.loop.call_later(5, | |
self._on_timeout) | |
if self.hdr: | |
self._timeout.cancel() | |
self.is_body = True | |
body = parse_body(data) | |
self._fut.set_result(body) | |
self.transp.write(data) | |
def connection_lost(self, exc): | |
if exc is not None: | |
print("Abnormal exit", exc) | |
self.transp = None | |
def read_hdr(self): | |
if self._fut: | |
raise Exception() | |
self._fut = asyncio.Future(loop=self.loop) | |
return self._fut | |
def read_body(self): | |
self._fut = asyncio.Future(loop=self.loop) | |
return self._fut | |
tr, pr = await loop.create_connection( | |
lambda: Proto(config), 'localhost', 1234) | |
tr.write() | |
tr.close() | |
while True: | |
with timeout(10): | |
hdr = await reader.read(1024) | |
parse_hdr(hdr) | |
with timeout(5): | |
body = await reader.read(hdr.size) | |
hdr -> body -> hdr | |
hdr = await pr.read_hdr() | |
hdr = await pr.read_hdr() # body | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class A: | |
__slots__ = ('val', ) | |
def __init__(self): | |
self.val = 0 | |
def f(self): | |
self.val | |
a = A() | |
a.val a.__dict__['val'] | |
assert type(a) == A == a.__class__ | |
typedef struct { | |
PyObject_HEAD | |
/* Type-specific fields go here. */ | |
// int a; | |
PyObject *obj; | |
double ob_fval; | |
} noddy_NoddyObject; | |
static PyMemberDef Noddy_members[] = { | |
{"fval", T_DOUBLE, offsetof(Noddy, ob_fval), 0, | |
"noddy number"}, | |
{"val", T_OBJECT, offsetof(Noddy, obj), 0, | |
"noddy number"}, | |
{NULL} /* Sentinel */ | |
}; | |
static PyTypeObject noddy_NoddyType = { | |
PyVarObject_HEAD_INIT(NULL, 0) | |
"noddy.Noddy", /* tp_name */ | |
sizeof(noddy_NoddyObject), /* tp_basicsize */ | |
0, /* tp_itemsize */ | |
0, /* tp_dealloc */ | |
0, /* tp_print */ | |
0, /* tp_getattr */ | |
0, /* tp_setattr */ | |
0, /* tp_reserved */ | |
0, /* tp_repr */ | |
0, /* tp_as_number */ | |
0, /* tp_as_sequence */ | |
0, /* tp_as_mapping */ | |
0, /* tp_hash */ | |
0, /* tp_call */ | |
0, /* tp_str */ | |
0, /* tp_getattro */ | |
0, /* tp_setattro */ | |
0, /* tp_as_buffer */ | |
Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
"Noddy objects", /* tp_doc */ | |
0, | |
}; | |
static PyTypeObject noddy_NoddyType = { | |
PyVarObject_HEAD_INIT(NULL, 0) | |
.tp_name = "noddy.Noddy", /* tp_name */ | |
.tp_basic_size = sizeof(noddy_NoddyObject), /* tp_basicsize */ | |
.tp_flags = Py_TPFLAGS_DEFAULT, /* tp_flags */ | |
.tp_doc = "Noddy objects", /* tp_doc */ | |
.tp_members = Noddy_members, | |
.tp_methods = Noddy_methods, | |
.tp_new = PyType_GenericNew, // __new__ | |
}; | |
static PyObject * | |
Noddy_name(Noddy* self, PyObject* args) | |
{ | |
PyArgs_ParseTuple(arg, "sO", ...); | |
} | |
static PyMethodDef Noddy_methods[] = { | |
{"name", (PyCFunction)Noddy_name, METH_VARARGS, | |
"Return the name, combining the first and last name" | |
}, | |
Noody(1.2) | |
Noody(val=1.2) | |
static int | |
Noddy_init(Noddy *self, PyObject *args, PyObject *kwds) | |
{ | |
PyObject * obj = NULL; | |
static char *kwlist[] = {"obj", "val", NULL}; | |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "Od|", kwlist, | |
&obj, | |
&self->ob_fval)) | |
return -1; | |
if(!PyList_Check(obj)) { | |
PyErr_SetString(PyExc_ValueError, "obj should be a list"); | |
goto fail; | |
} | |
self->obj = obj; | |
PY_INCREF(self->obj); | |
return 0; | |
fail: | |
PY_XDECREF(obj); | |
return -1; | |
} | |
def __init__(self, v): | |
self.val = v | |
if not isinstance(self.val, list): | |
raise ValueError | |
def __init__(self, v): | |
if not isinstance(v, list): | |
raise ValueError | |
self.lst.append(v) | |
static void | |
Noddy_dealloc(Noddy* self) | |
{ | |
Py_XDECREF(self->obj); | |
Py_TYPE(self)->tp_free((PyObject*)self); | |
} | |
tp_traverse | |
static int | |
Noddy_traverse(Noddy *self, visitproc visit, void *arg) | |
{ | |
Py_VISIT(self->obj); | |
return 0; | |
} | |
tp_clear | |
static int | |
Noddy_clear(Noddy *self) | |
{ | |
Py_CLEAR(self->obj); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment