Probably everyone has already heard about the wonderful library
asyncio ?
If not, then briefly: this has become the standard for asynchronous network programming in Python.
tornado and
twisted or learn how to work with it or gradually go to the outcasts.
asyncio is beautiful, but it's just a library for working with
TCP ,
UDP ,
UNIX sockets,
PIPES, and asynchronous
subprocess .
')
So that everyone needs cool third-party libraries that can work with
asyncio . Something is already there, but not enough.
So I made a couple with my colleagues: one for
ZeroMQ and another for
PostgreSQLIf you sit firmly on Python 2 and are not interested in Python 3 - do not go under the cat in order to avoid stupid questions and other misunderstandings .
Why did I need it?
Because we are starting a new project in which
ZeroMQ and
Postgress are indispensable. I'd like to do on
asyncio . I had to gash the missing pieces.
I must say, both of them are good. I am
Python Core Developer and in the
asyncio code
there is also a lot of my work. I think I understood how to write well for the system, which I myself helped create.
Plus (almost) 100% code coverage of both libraries with tests and intelligible (hopefully) documentation.
aiozmq - allows you to use
ZeroMQ sockets with
asyncio .
Documentation is
here .
aiozmq works with low-level
ZeroMQ sockets and, most importantly, out of the box gives the
Remote Procedure Call mechanism (plus
PubSub and
Notify ).
Short example:
import asyncio import aiozmq import aiozmq.rpc class ServerHandler(aiozmq.rpc.AttrHandler): @aiozmq.rpc.method def remote_func(self, a:int, b:int) -> int: return a + b @asyncio.coroutine def go(): server = yield from aiozmq.rpc.start_server( ServerHandler(), bind='tcp://127.0.0.1:5555') client = yield from aiozmq.rpc.open_client( connect='tcp://127.0.0.1:5555') ret = yield from client.rpc.remote_func(1, 2) assert 3 == ret server.close() client.close() asyncio.set_event_loop_policy(aiozmq.ZmqEventLoopPolicy()) asyncio.get_event_loop().run_until_complete(go())
We now turn to
aiopgThis lib can work with
psycopg2 in asynchronous mode and gives a
connection pool as a nice bonus.
Documentation is
here .
@asyncio.coroutine def test_select(): pool = yield from aiopg.create_pool(dsn) with (yield from pool.cursor()) as cur: yield from cur.execute('SELECT 1') ret = yield from cur.fetchone() assert ret == (1,), ret
Both libraries are posted on GitHub:
aiozmq and
aiopgTry it, if you like it - use it.
Find bugs - write on githubs in the
issues , or better yet, just
pull request