Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add asyncio event loop support #4071

Merged
merged 14 commits into from Apr 20, 2015
Merged

Add asyncio event loop support #4071

merged 14 commits into from Apr 20, 2015

Conversation

takluyver
Copy link
Contributor

This probably needs quite a bit of work, but it's working for a basic example, and I wanted to communicate early. @techdragon talked me into working on this at the PyCon sprints.

Asyncio is part of the standard library in Python 3.4. It's a rather similar API to tornado, so I copied and pasted the net_tornado code that's already in rethinkdb and modified it until it works. The syntax is only valid on Python 3, but the module shouldn't be imported until you do r.set_loop_type('asyncio'), so that shouldn't be a problem.

Here's the asyncio version of the tornado example from your async drivers blog post (BTW, that example is missing a colon on the while line):

import asyncio
import rethinkdb as r

r.set_loop_type("asyncio")

@asyncio.coroutine
def print_changes():
    conn = yield from r.connect(host="localhost", port=28015)
    feed = yield from r.table("tv_shows").changes().run(conn)
    while (yield from feed.fetch_next()):
        change = yield from feed.next()
        print(change)

loop = asyncio.get_event_loop()
loop.run_until_complete(print_changes())

@danielmewes
Copy link
Member

That's really cool, thanks a lot for the pull request @takluyver !

@Tryneus I think this is one for you to review. Can you take a look when you find the time?

@takluyver Have you signed our CLA yet? http://rethinkdb.com/community/cla/

@danielmewes danielmewes added this to the 2.1-polish milestone Apr 16, 2015

def _empty_error(self):
# We do not have RqlCursorEmpty inherit from StopIteration as that interferes
# with Tornado's gen.coroutine and is the equivalent of gen.Return(None).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be asyncio, not Tornado - and the gen.Return(None) thing isn't quite true when using yield from.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same problem would still arise - return foo in a coroutine still works by raising StopIteration(foo), I believe. I'll update the comment.

@Tryneus
Copy link
Member

Tryneus commented Apr 16, 2015

@takluyver, thanks for putting this up, it looks great, although I made a few comments/suggestions.

@takluyver
Copy link
Contributor Author

Thanks, I was fully expecting that it wouldn't be quite right. I'll respond to comments inline.

@danielmewes I just went and signed the CLA now. The address field mentioned that you might send me something - if that's a t-shirt, then save yourselves the postage, because I already picked up two at PyCon. ;-)

@danielmewes
Copy link
Member

Good to know about the shirts @takluyver :)


return (sum(cursor_counts), sum(cursor_timeouts))

# @asyncio.coroutine
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason these tests are commented out? Were they taking too long? If I remember correctly they're the longest-running tests in the suite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I was commenting out chunks of the tests just so that I could run them quickly. I didn't mean to commit them commented out.

@takluyver
Copy link
Contributor Author

Tests are now uncommented and passing, and I think I've addressed all of the review comments so far.

@Tryneus
Copy link
Member

Tryneus commented Apr 20, 2015

Awesome, looks great, @takluyver! Merging this into next, it should make it into the 2.1 release. I'll try to get more tests in for the alternate net.py implementations before then.

Thanks for all the help!

Tryneus added a commit that referenced this pull request Apr 20, 2015
Add asyncio event loop support
@Tryneus Tryneus merged commit 5c6a4e9 into rethinkdb:next Apr 20, 2015
@takluyver
Copy link
Contributor Author

You're welcome :-)

@takluyver takluyver deleted the asyncio branch April 20, 2015 19:00
@danielmewes danielmewes modified the milestones: 2.2-polish, 2.1 May 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants