-
-
Save asvetlov/89572c95834b6ad2183856cdb3e92e19 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
async def f(url): | |
txt = await get_url(url) | |
return txt | |
task = ensure_future(f()) | |
await task | |
tasks = [t] | |
for t in tasks: | |
await t | |
r1, r2, r3 = await asyncio.gather(f(u1), f(u2), f(u3)) | |
async def main(): | |
args = parser.parse_args() | |
await f() | |
await sleep(0, loop=loop) | |
async def sleep(delay, loop=None): | |
if loop is None: | |
loop = asyncio.get_event_loop() | |
... | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
loop.close() | |
class A: | |
def __init__(self): | |
self.client = aiomcache.Client() | |
async def get(self, key): | |
return await self.client.get(key) | |
async def f(): | |
a = A() | |
val = await a.client.get('key') | |
# pytest-aiohttp | |
async def test_a(loop): | |
a = A() | |
A.client = aiomcache.Client(loop=loop) | |
val = await a.client.get('key') | |
assert val == '123' | |
tasks = [] | |
await asyncio.wait(tasks) | |
await asyncio.wait_for(f(), 5) | |
import async_timeout | |
async def g(): | |
with async_timeout.timeout(6): | |
await f() | |
sdfwefwe | |
324234 | |
with contextlib.closing(await open_stream()) as stream: | |
await stream.read() | |
stream = contextlib.closing(await open_stream()) | |
try: | |
await stream.read() | |
finally: | |
stream.close() # no await | |
while tasks: | |
done, tasks = await asyncio.wait(tasks, timeout=12) | |
for t in done: | |
await t | |
for f in asyncio.as_completed(tasks): | |
val = await f | |
in_q = asyncio.Queue(maxsize=100) | |
out_q = asyncio.Queue(maxsize=100) | |
async def f(in_q, out_q): | |
url = await in_q.get() | |
if url is None: | |
return | |
txt = await get_url(url) | |
await out_2.put(txt) | |
async def g(in_q, out_q) | |
txt = await out_q.get() | |
for u2 in parse(txt): | |
await in_q.put(u2) | |
async def put(self, val): | |
while True: | |
if self.full(): | |
await sleep(0) | |
else: | |
self._q.append(val) | |
return | |
f = Future() | |
await f | |
f.set_result(123) | |
f.set_exception(ValueError()) | |
f.done() | |
f.add_done_callback(cb) | |
await f | |
def cb(f): | |
res = f.result() | |
await coro() | |
async def f(url): | |
while True: | |
try: | |
await get_url(url) # raise asyncio.CancelledError | |
finally: | |
await redis.close() | |
task = await ensure_future(f(url)) | |
task = await loop.create_task(f(url)) | |
task.cancel() | |
await sleep(0) | |
PYTHONASYNCIODEBUG=1 python 1.py | |
loop.set_debug(True) |
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 async_timeout | |
class Redis: | |
def __init__(self): | |
self._parser = hiredis.HiRedis() | |
async def _init(self, host, port): | |
self._reader, self._writer = await open_connection( | |
host, port) | |
self._task = ensure_future(self._do_read()) | |
self._q = [] | |
async def close(self): | |
self._task.cancel() | |
with contextlib.suppress(asyncio.CancelledError): | |
await self._task | |
self._writer.close() | |
self._q = None | |
self._reader = self._writer = None | |
def execute(self, *args): | |
if self._writer is None: | |
raise RuntimeError("Connection is closed") | |
msg = self._make_msg(args) | |
return asyncio.ensure_future(self._write(msg)) | |
async def _write(self, msg): | |
f = asyncio.Future() | |
self._writer.write(msg) | |
with async_timeout.timeout(60): | |
await self._writer.drain() | |
self._q.append(f) | |
return await f | |
async def _do_read(self): | |
while True: | |
try: | |
banswer = await self._reader.read(1024) | |
# except asyncio.CancelledError: | |
# pass | |
except Exception as exc: | |
for f in self._q: | |
f.set_exception(exc) | |
await self.close() | |
return | |
self._parser.feed(banswer) | |
answer = self._parser.get() | |
while answer is not None: | |
f = self._q.pop(0) | |
f.set_result(answer) | |
answer = self._parser.get() | |
async def redis(host, port): | |
redis = Redis() | |
return await redis._init(host, port) | |
async def main(): | |
redis = await redis('localhost', 6379) | |
try: | |
with async_timeout.timeout(1): | |
ret = await redis.execute(b'SET', b'KEY', b'VALUE') | |
assert None | |
ret = await redis.execute(b'GET', b'KEY') | |
assert b'VALUE' == ret | |
await redis.set('KEY', 'VALUE') | |
results = [] | |
for i in range(1000*1000): | |
results.append(redis.execute(b'SET', b'KEY', b'VALUE')) | |
await asyncio.gather(*results) | |
finally: | |
await redis.close() | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
loop.run_until_complete(main()) | |
loop.close() | |
$ PYTHONASYNCIODEBUG=x python 1.py | |
loop.set_debug(True) | |
loop.slow_callback_duration = 0.1 | |
async def f(): | |
requests.get("http://python.org") | |
f = Future() | |
f.set_exception(ValueError()) | |
del f |
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
PyObject * p; | |
"siO" < ("adsd", 1, A()) | |
f("adsd", 1, A()) | |
def f(*args): | |
pass | |
def append(l, v): | |
if not isinstance(l, list): | |
raise RuntimeError("sdfsd") | |
l.append(v) | |
return v | |
static PyObject * | |
append(PyObject* self, PyObject * args) | |
{ | |
PyObject * l=NULL; | |
PyObject * v=NULL; | |
_Py_IDENTIFIER(append); | |
static PyIdentifier * PyId_append = ...("append"); | |
if(!PyArg_ParseTuple(args, "OO", &l, &v)) | |
return NULL; | |
if(!PyList_Check(l)) { | |
PyErr_SetString(PyExc_RuntimeError, "first arg should be a list"); | |
goto err; | |
} | |
// l.append(v) | |
if(!PyObject_CallMethodObj(l, &PyId_append, "O", v)){ | |
goto err; | |
} | |
Py_XDECREF(l); | |
Py_INCREF(v); | |
Py_XDECREF(v); | |
return v; | |
err: | |
Py_XDECREF(l); | |
Py_XDECREF(v); | |
return NULL; | |
} | |
def append2(l, v): | |
if not isinstance(l, list): | |
raise RuntimeError("sdfsd") | |
l.append(v) | |
return None | |
static PyObject * | |
append(PyObject* self, PyObject * args) | |
{ | |
PyObject * l=NULL; | |
PyObject * v=NULL; | |
if(!PyArg_ParseTuple(args, "OO", &l, &v)) | |
return NULL; | |
if(!PyList_Check(l)) { | |
PyErr_SetString(PyExc_RuntimeError, "first arg should be a list"); | |
goto err; | |
} | |
// l.append(v) | |
if(!PyObject_CallMethod(l, "append", "O", v)){ | |
goto err; | |
} | |
Py_XDECREF(l); | |
Py_XDECREF(v); | |
Py_INCREF(Py_None); | |
return Py_None; | |
err: | |
Py_XDECREF(l); | |
Py_XDECREF(v); | |
return NULL; | |
} | |
PyObject * lst = PyList_New(5); | |
if(!lst) goto err; | |
int i; | |
fpr(i=0; i<5; i++) | |
Py_INCREF(Py_None); | |
if (!PyList_SetItem(lst, i, Py_None)) | |
goto err; | |
return lst; | |
static PyObject * | |
spam_system(PyObject *self, PyObject *args, PyObject* kwargs) | |
{ | |
const char *command; | |
int sts; | |
if (!PyArg_ParseTuple(args, "s", &command)) | |
return NULL; | |
Py_BEGIN_ALLOW_THREADS; | |
sts = system(command); | |
Py_END_ALLOW_THREADS; | |
if (sts < 0) { | |
PyErr_SetString(SpamError, "System command failed"); | |
return NULL; | |
} | |
return PyLong_FromLong(sts); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment