You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Twisted port of the Python driver's connection class #3785
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
importrethinkdb-twistedasr
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.
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.
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.
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) ?
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 .
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 !)
@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.
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.
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.
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:
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) :
forchangeincursor:
print (yieldchange)
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 :) !
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!
@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.
Activity
danielmewes commentedon Feb 16, 2015
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
onon a cursor that's on a connection using the default (synchronous) backend should just throw an error I think.danielmewes commentedon Feb 16, 2015
Alternatively we could support Tornado first (#2622 (comment)). The same questions regarding the API and module interface as here would apply.
danielmewes commentedon Feb 16, 2015
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.
RaitoBezarius commentedon Apr 18, 2015
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!
danielmewes commentedon Apr 20, 2015
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 intest/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 .
RaitoBezarius commentedon Apr 21, 2015
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 !)
danielmewes commentedon Apr 21, 2015
@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_*.pyfile).coffeemug commentedon Apr 22, 2015
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 isimport rethinkdb.twisted as r, which I think is more pythonesque thanimport rethinkdb_twisted as r.danielmewes commentedon Apr 22, 2015
Actually I didn't intend this to be a ReQL_proposal. We already have
set_async_backendwhich 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.
danielmewes commentedon Apr 22, 2015
set_loop_typeI meant, notset_async_backend(but it's the same thing).http://rethinkdb.com/api/python/set_loop_type/
RaitoBezarius commentedon Apr 22, 2015
@danielmewes I wanted to ask you about the behavior of the cursors, I think that they are not very very async-friendly.
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: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) :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 :) !
Tryneus commentedon Apr 22, 2015
@RaitoBezarius, a few things:
You're right that
_maybe_fetch_batchreceives 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 withnoreply=True, and it returnsNone. 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
DefaultCursorimplements iteration, but we had problems getting iteration to work onTornadoCursorbecause Tornado coroutines are generators - and aStopIterationmeans 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!
RaitoBezarius commentedon Apr 22, 2015
@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.
danielmewes commentedon Jul 27, 2015
@RaitoBezarius 's implementation has been merged and will ship with RethinkDB 2.1.
#4096