async-tokio - asyncio + rust tokio

443 views
Skip to first unread message

Nikolay Kim

unread,
Mar 29, 2017, 7:43:33 PM3/29/17
to aio-libs
about week ago I started new project async-tokio https://github.com/fafhrd91/async-tokio
this is asyncio event loop written in rust language http://www.rust-lang.org based on tokio framework.
it is in very early stage, but it is possible to start aiohttp.web app https://github.com/fafhrd91/async-tokio/blob/master/tests/test_basic.py#L234

I think right now we have very big gap in toolchain related to high performance apps,
we have uvloop and all other optimized libraries, but none of those provide solution for application code.
and writing cython or c-api code for python is not fun. on other hand rust is modern language and has very good
c-level integration story. one of the ideas for async-tokio is to build tool that would allow to mix python and rust code
in one web app. I am planing to run async-tokio in production next week,
of cause not as full asyncio loop, but as simple socket server with custom frame parsing. 
anyone wants to join? as a side note, i already use python extensions written in rust in production, everything works well.

Nikolay Kim

unread,
Apr 10, 2017, 6:15:47 PM4/10/17
to aio-libs
Some progress updates.


Initially I thought about fully functional event loop, but it seems it is more valuable to use it as library, and make your our specific loops or transports. It is very easy to make new transports, reactive nature of asyncio helps a lot. Simple protocol contains only three major method connection_made, connection_lost, data_received. So i can do any type of parsing and data management and then send to data_received already structured data.
Right now I am working on python2.7+gevent app, and I fully reuse a async-tokio implementation. It uses thrift protocol, I am moving serialization/deserialization process out of python into separate thread. Relatively easy task, especially with rust memory guaranties.

Async-tokio is in state that it can run aiohttp without problems. Performance is comparable to uvloop. I also tried to run tokio loop in separate thread from aiohttp handler, works fine but performance is similar, seems at this point performance largely limited by python itself. But it should be relatively easy to write RequestHandler and payload stream in rust and then maybe running io in separate thread would make more sense

Other updates. I added awaitable support to rust-cpython, so it is possible to create awaitable objects in rust. Also added buffer protocol support, so you don't need to copy rust memory to bytes object to expose it to python, it is possible directly access memory from python

I separated async-tokio into two repos
async-tokio is rust library
And aiohttp-tokio is concrete python extension


Ludovic Gasc

unread,
Apr 10, 2017, 6:37:06 PM4/10/17
to Nikolay Kim, aio-libs
Hi Nikolay,

It seems a little bit crazy at the first glance, but at same time, it seems to work pretty well for you.

I'm really curious if at one moment, you have time to share with us a "step-by-step" guide to launch that and run, for example, an aiohttp example.

Or, in fact, it's like uvloop, it simply an import to add and forget ?
Sorry, but I never use Rust, it isn't really clear to me how it works together.

Anyway, thanks to share your discoveries.
--
Ludovic Gasc (GMLudo)
Lead Developer Architect at ALLOcloud

--
You received this message because you are subscribed to the Google Groups "aio-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aio-libs+unsubscribe@googlegroups.com.
To post to this group, send email to aio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/aio-libs.
To view this discussion on the web visit https://groups.google.com/d/msgid/aio-libs/C33AA13F-129B-44EA-A7D1-5CDD385FF90C%40gmail.com.

For more options, visit https://groups.google.com/d/optout.

Nikolay Kim

unread,
Apr 10, 2017, 6:52:17 PM4/10/17
to Ludovic Gasc, aio-libs
All io happen in separate thread controlled by async-tokio.

I think a lot of developers do not chose python for web services because they afraid to be locked by python infrastructure. 
If you hit limits of python, there is no escape path, only to re-write application in different language. I see async-tokio as an escape path.
With it you can mix and match python and rust, and use best from both worlds. Of cause you can use c++ or cython or c, but it is much nicer to use modern language.

Ludovic Gasc

unread,
Apr 12, 2017, 10:00:54 AM4/12/17
to Nikolay Kim, aio-libs
Thanks for the explanations.

Is it possible to use at the same time async-tokio and C code like psycopg2 via aiopg ?

If yes, I will add your tool in the TechEmpower FrameworkBenchmarks ;-)
--
Ludovic Gasc (GMLudo)
Lead Developer Architect at ALLOcloud

Nikolay Kim

unread,
Apr 12, 2017, 11:08:58 AM4/12/17
to Ludovic Gasc, aio-libs
Async-tokio does not implement complete event loop api, I do not think aiopg will work. I will work on full loop implementation later.
But it is possible to run http io in separate thread, and run aiopg with uvloop or default loop. I am planing to implement 
Request handler and http parsing in rust, so it should be possible to offload more to separate thread.

Ludovic Gasc

unread,
Apr 14, 2017, 1:17:34 PM4/14/17
to Nikolay Kim, aio-libs
Ok, it's completely clear to me now.

I'll keep an eye about the evolution. 
--
Ludovic Gasc (GMLudo)
Lead Developer Architect at ALLOcloud

Nikolay Kim

unread,
Apr 23, 2017, 8:36:33 PM4/23/17
to aio-libs
Updates on async-tokio state

Recently I added support for open_connetion api, that should cover a lot of client libraries like aiomcache etc. I didn't test those libraries yet, but most of aiohttp client functional tests pass, except some of the ssl related tests. But that's just compatibility changes, should be possible to fix. Btw ssl is supported on client side, should be easy to add on server as well. While implementing custom task class I discovered some of asyncio shortcomings, like it is not possible to participate in Task.current_task api, or asyncio.gather uses a lot of private attributes, so you have to monkey patch it.

Server is interesting. I implemented http headers parsing and PayloadWriter in rust plus some of the glue code. This gives good amount of speedup. For very simple requests handling, numbers on default loop about 10k for non pipelined and 13k for pipelined requests per seconds, for uvloop about 13 and 16 respectively. For custom rust code numbers are 18k and 27k rps and there are a lot of space for improvements.

Initially I though that implementing general purpose event loop would be complex task, but actually it is not that difficult. Biggest missing point right now is signal handlers. I am not sure about subprocess, pipes and udp. But and pipes should be relatively easy to add. 

Well, in any case help would be very welcome.


Sent from my iPhone

Nikolay Kim

unread,
Apr 28, 2017, 5:53:01 PM4/28/17
to aio-libs
News on async-tokio project.

More developers decided to join effort on async-tokio. Specifically Yury Selivanov, creator of uvloop and current BDFL for asyncio.
We decided to move development to dedicated GitHub organization. 


Project name is PyO3 (Pythonium Trioxide)

Welcome everyone to join our effort.

Nikolay Kim

unread,
May 9, 2017, 11:03:42 PM5/9/17
to aio-libs
Some updates on async-tokio project.

Tokio loop passes all aiohttp tests!
It is about 1750 test, I hade to skip tests that monkey patch loop method, but that's about 30 tests.

What is done:

* tcp api is ready, client and server (ssl as well)
* sock_xxx api 
* unix domain sockets
* signals
* timer api (call_at, call_later, etc)

Next in pipeline: pipes, subprocesses, and udp

I need help from aio libs maintainers, I just need to check if tests work with tokio loop.

And we decided to rename project to tokio

I hope to complete all ground work in couple weeks and make 0.1 release

Ah, forgot to add. I added GunicornTokioWebWorker to aiohttp, so it should be relatively easy to run aiohttp with tokio loop

Sent from my iPhone

Ludovic Gasc

unread,
May 10, 2017, 2:23:08 AM5/10/17
to Nikolay Kim, aio-libs
I'll test that soon ;-)

Ludovic Gasc (GMLudo)
http://www.gmludo.eu/

--
You received this message because you are subscribed to the Google Groups "aio-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aio-libs+unsubscribe@googlegroups.com.
To post to this group, send email to aio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/aio-libs.

Nikolay Kim

unread,
May 10, 2017, 3:49:13 PM5/10/17
to aio-libs
tokio 0.1.0 is released. 

most of apis are implemented, except udp. 
now it is time for polishing.

Tokio depends on rust-cpython crate, I have to use fork because author does not respond.
so maybe we should make proper fork.

a lot of work. so please join my effort :)
Reply all
Reply to author
Forward
0 new messages