Skip to content

Twisted port of the Python driver's connection class #3785

Closed

Description

@danielmewes

This is a follow-up of #3298 .

We will be providing an on(cb) function on cursors, and integrate with the framework's event loop to call the callback once we get a response.

This issue is for a Twisted-based implementation. Unless Twisted provides another natural way of handling errors, we should probably just make the cb a function on (error, row) like in the JS driver.

An open question is how to package the Twisted driver variant. The easiest way probably is to have a separate module

import rethinkdb-twisted as r

that is equivalent to rethinkdb except for using Twisted for i/o.

Another alternative is to have a special connection type, e.g. r.connect(host, backend="twisted"). That's a bit nicer to use I think, but I don't know how much work it is to get the driver module import properly on a system that doesn't even have Twisted installed.

Pinging @gchpaco @timmaxw @larkost for comments.

Activity

  1. added this to the 2.0 milestone on Feb 16, 2015
  2. danielmewes commented on Feb 16, 2015

    @danielmewes
    MemberAuthor

    Also note #2622 (comment) regarding multiple backend.

    If at all possible I think we should provide the r.connect(host, backend="twisted") interface (or something similar to that) rather than a separate module.

    Calling on on a cursor that's on a connection using the default (synchronous) backend should just throw an error I think.

  3. danielmewes commented on Feb 16, 2015

    @danielmewes
    MemberAuthor

    Alternatively we could support Tornado first (#2622 (comment)). The same questions regarding the API and module interface as here would apply.

  4. danielmewes commented on Feb 16, 2015

    @danielmewes
    MemberAuthor

    Decision is in: We're going to work on the Tornado integration first, and then see if we have time left to do Twisted for 2.0 as well or if we will wait for that until a little later.

  5. modified the milestones: , 2.0 on Feb 16, 2015
  6. modified the milestones: , on Apr 14, 2015
  7. RaitoBezarius commented on Apr 18, 2015

    @RaitoBezarius
    Contributor

    Are there any updates concerning this issue? I can see that now, it's planned for 2.0.x or 2.1?
    I wanted to know what would be needed to make it work, except implementing a net_twisted.py (like net_tornado.py) ?

    Thanks!

  8. danielmewes commented on Apr 20, 2015

    @danielmewes
    MemberAuthor

    Hi @RaitoBezarius, when we are going to ship this will depend on how much demand there is for this backend.
    Implementing it should just need a net_twisted.py, and a few tests for it (like the one in test/rql_test/connections/tornado_connection.py). We're happy to support you if you want to give it a shot!
    For another data point, see this pull request by @takluyver for a Python 3 async backend: #4071 .

  9. RaitoBezarius commented on Apr 21, 2015

    @RaitoBezarius
    Contributor

    Thanks @danielmewes! Is there some protocol spec or a general description of how a client driver should interact with the server and what kind of assumptions can the client make? (Anyway, I can just use those implementation details to work with).

    EDIT: Here you can take a look at a first implementation of the driver: RaitoBezarius@8fd8103 (But didn't test yet !)

  10. danielmewes commented on Apr 21, 2015

    @danielmewes
    MemberAuthor

    @RaitoBezarius Very cool. I'm looking forward to your pull request. Feel free to put it up even before it's completely ready If you like, so we can give you early feedback.

    We have this article on how the protocol works http://rethinkdb.com/docs/writing-drivers/ . I think for porting to Twisted you probably won't need most of this, since those things are taken care of in the higher levels of the driver (not in the net_*.py file).

  11. coffeemug commented on Apr 22, 2015

    @coffeemug
    Contributor

    IMO r.connect(backend=X) is better than a separate module, but what happens if people start two connections with different backends? Will they interop properly?

    One other option is r.set_async_backend(X), which I think is a good choice. Yet another option is import rethinkdb.twisted as r, which I think is more pythonesque than import rethinkdb_twisted as r.

  12. danielmewes commented on Apr 22, 2015

    @danielmewes
    MemberAuthor

    Actually I didn't intend this to be a ReQL_proposal. We already have set_async_backend which we're going to use for all the new Python backends for the time being.
    I'd prefer to not discuss the specifics of the Twisted API as part of the ReQL discussion period.

    Sorry this slipped through. Removing the ReQL_proposal label.

  13. RaitoBezarius commented on Apr 22, 2015

    @RaitoBezarius
    Contributor

    @danielmewes I wanted to ask you about the behavior of the cursors, I think that they are not very very async-friendly.

        def _maybe_fetch_batch(self):
            if self.error is None and \
               len(self.items) <= self.threshold and \
               self.outstanding_requests == 0:
                self.outstanding_requests += 1
                self.conn._parent._continue(self)

    Currently, _continue calls run_query which should return a Deferred/Future/Whatever.

    But as we don't return it from _maybe_fetch_batch, thus, it is impossible to get the deferred object.
    Furthermore, I think that there is a biggest problem (correct me if what I'll say is wrong).

    Cursor works by waiting for the connection to extend them to get a new response.
    In order to iterate over a cursor, we wait that those items are extended by the connection.
    But how we wait during iteration: while len(self.items) == 0. With an async backend, it's maybe stupid to it like that, we should just wait for the deferred from _maybe_fetch_batch returns or sleep until we're extended:

    def _get_next(self, timeout):
        start_timeout(timeout, self.new_response)
        yield self.extended
        returnValue(convert_pseudo(self.items.pop(0), self.query))

    In fact, all _maybe_fetch_batch is to send CONTINUE query if I understand correctly?

    It would clearly better if we implemented cursor just as a generator, by subclassing __iter__ and __next__, leading to a code (independent from any backend) :

    for change in cursor:
        print (yield change)

    It was a lot of thoughts in one message, sorry for it!

    EDIT: You can have a working version of the Twisted driver here: RaitoBezarius@8cd55fc
    My PR is coming :) !

  14. Tryneus commented on Apr 22, 2015

    @Tryneus
    Member

    @RaitoBezarius, a few things:

    You're right that _maybe_fetch_batch receives a future in an async environment, but at the moment it is always immediately available, so it doesn't cause any problems. The CONTINUE query is sent with noreply=True, and it returns None. We don't wait on these futures when sending the CONTINUE query because the user should only wait when consuming items from the cursor, not when creating it.

    The DefaultCursor implements iteration, but we had problems getting iteration to work on TornadoCursor because Tornado coroutines are generators - and a StopIteration means that the coroutine is finished and has no more futures to process. I couldn't find a safe way to have an iterator implemented in terms of a Tornado coroutine.

    Looking forward to your PR, let me know if you have any questions!

  15. RaitoBezarius commented on Apr 22, 2015

    @RaitoBezarius
    Contributor

    @Tryneus Thanks for your answer.
    I've managed to get the iteration working here: RaitoBezarius@3abeec4
    But, it yields deferred, and all I need to do is to yield them again.

    As for tests, should I implement as many tests as there is for Tornado and Asyncio?
    I'll open the PR.

  16. danielmewes commented on Jul 27, 2015

    @danielmewes
    MemberAuthor

    @RaitoBezarius 's implementation has been merged and will ship with RethinkDB 2.1.
    #4096

  17. modified the milestones: 2.1, 2.2-polish on Jul 27, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions