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
The next operation in Python on cursors is a blocking operation, making it impossible to read from multiple changefeeds at once. This makes it very hard to take full advantage of changefeeds in Python.
I believe callbacks would solve the problem nicely. We could have some sort of Deferred objects à la twisted, but I personally don't like that API. I am not familiar with what other Python libraries do.
The closest match in the general library are the Queue classes, and they provide methods like Queue.get_nowait() or Queue.get(block=True, timeout=x) which raise a Queue.Empty if there is nothing available. It is too bad that the Queue classes are not a subclass of iterables because that would make a great model for us.
Alternately we could forge a new path and have next raise StopIteration when there is nothing available without closing the cursor. That would be really non-standard, but make for clean-looking code and would probably require an opt-arg to not break existing code.
I think the best is probably to emulate Queue for this type of behavior.
I would not use Queue. It is very heavyweight and is designed to be used in multi-threaded code, which the driver doesn't support. It would not work in single-threaded code because both put and get could block the thread indefinitely.
@mlucy's idea of using callbacks seems best to me. It is the most general. Users could easily use a queue by adding q.put as a callback.
@AtnNn You are mistaking my suggestion: I was suggesting that if we are looking for a model that we emulate the interface of Queue. It is not the cleanest out there, but it is one that Python programmers trying to do this sort of thing are already accustomed to. We don't really need normal get or put since we already have that (hence my comment about Queue not having been modeled as an extension of iterables), but the get_nowait() call is essentially what you are asking for. Extending cursors this way would be a clean way of getting this model.
Callbacks are not often used in Python outside a few packages and so are a poor choice.
Sorry, I did misunderstand your proposal. It would be nice to have next_nowait, but I don't see how it would solve this issue. I have realised callbacks would not work either without an event loop.
asyncio seems to be Python's answer to this issue, as discussed in #2622. We could bite the bullet and rewrite the driver to use that model.
The Ruby driver also has the limitation described in this issue.
asyncio is new in Python3.4, and is subject to much change even there. Additionally it is an event-loop based system, so would be a very different model than our current driver. If that was the direction we want to go then we would be better off going with something that is more portable like twisted (call back based) or greenlets(would non standard package with monkey-patching). But either of those would be almost complete driver re-writes, and so would be better to have a either community or secondary drivers.
But I don't see how the get_nowait() method does not solve the problem blocking operations. To illustrate...
Here you will see a value from either, both, or neither depending on what is already available. In a real example you would wrap this in a while block with some appropriate sleeps depending on where you got data.
Now I believe that there are more problems lurking below the surface in how the server actually fulfills these requests, but am a little foggy about the conversation I had with @mlucy about that a while ago.
Oh, and to add the example for the second possible solution that I posted earlier:
feedA=r.table('a').changes(someFlag=True).run(conn)
feedB=r.table('b').changes(someFlag=True).run(conn)
whileTrue:
forainfeedA: # would get StopIteration error if there is nothing available at that momentprint(a)
forbinfeedB: # would get StopIteration error if there is nothing available at that momentprint(b)
Important to note here is that even though they emit StopIteration errors (how iterators signal they are done), both feedA and feedB would still be capable of returning further results. This would be very non-standard behavior in Python, but it is an elegant solution.
Edit: the major problem here is how do we signal when a changefeed is satisfied? (e.g.: one with a limit)
A get_nowait operation would still have to block while asking the server if there are any more available documents. I would prefer a solution that does not block.
wrap this in a while block with some appropriate sleeps
That would be ugly and would work in some cases, but when reading from a real-time feed, any form of sleep is inappropriate.
I think get_nowait() solves the problem. We could add a conn.wait_any() function on the connection, that would wait until there's new data for any of the active queries to avoid the constant sleep in the outer loop.
One thing that might make callbacks slightly nicer is that code that uses change feeds can be decoupled better.
With the get_nowait(), you need some kind of central place that repeatedly calls everything that might have a change feed open. With a callback, the connection is the only thing in your application that has to keep track of who currently has changefeeds running.
So I imagine that callbacks could be easier to use if you have many different components in your application accessing RethinkDB, and not one place that coordinates all of them.
Could be support both interfaces? A Queue-like one, and a callback one?
Solving this through StopIteration is not only non-standard but it would prevent these iterators being used in conjunction with the itertools module for example, we really shouldn't go down that path.
I don't think that that is a requirement. Rather get_nowait() would only check if there were already received responses. In this model we would have outstanding requests for any active changefeeds and either use any call (e.g.: next or get_nowait) as a chance to do general housekeeping work such as processing arrived packets and then do the check in the cursor. Or we separate that into its own background thread simplifying some parts and complicating others. But in either case it would still solve the presented problem.
@danielmewes I am not sure what you mean. So far I think that the Queue-like idea is completely orthogonal to callbacks, which would require a runloop.
and similarly for the others. That is, they will automatically play nice with async stuff under the scenes, so you don't need to use arun and aconnect and the like unless actually necessary.
Count me onboard for the callbacks for changefeeds option. I'm concerned about making the basic "just get started" version significantly different than the code you'd need to write real applications in. It seems the r.join syntax does force the user to write a select loop which is annoying and handled by all of these async libraries already.
Ultimately, the users and the driver will be gravitating towards asyncio over time, so it makes sense to align the driver with that api vs. inventing something specific for our needs. A lot of time and thought went into the asyncio interface, and we probably shouldn't waste time coming up with something unique.
I would make conn.loop_forever() exist, but be a direct call into run_forever() on the associated event loop. Easier to explain to new users, does the same thing.
In an offline discussion with @larkost , @coffeemug , @timmaxw and @mlucy , we decided to provide an event-loop integration already in 2.0.
Apart from #3529 , this is going to be the only way to read from multiple changefeeds at the same time in Python.
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.
An open question was which frameworks we should support first.
Twisted seems to be very widely used and easily available, so I think we should definitely support that.
As a second one @gchpaco suggested asyncio for Python >= 3.4 and the similar Trollius for older versions of Python. It probably makes sense to support that second.
Activity
mlucy commentedon Nov 6, 2014
What would we do to improve this? Add a way to have a callback be called whenever there's a new change?
AtnNn commentedon Nov 6, 2014
I believe callbacks would solve the problem nicely. We could have some sort of Deferred objects à la twisted, but I personally don't like that API. I am not familiar with what other Python libraries do.
larkost commentedon Nov 7, 2014
The closest match in the general library are the
Queueclasses, and they provide methods likeQueue.get_nowait()orQueue.get(block=True, timeout=x)which raise aQueue.Emptyif there is nothing available. It is too bad that theQueueclasses are not a subclass of iterables because that would make a great model for us.Alternately we could forge a new path and have
nextraiseStopIterationwhen there is nothing available without closing the cursor. That would be really non-standard, but make for clean-looking code and would probably require an opt-arg to not break existing code.I think the best is probably to emulate
Queuefor this type of behavior.AtnNn commentedon Nov 7, 2014
I would not use Queue. It is very heavyweight and is designed to be used in multi-threaded code, which the driver doesn't support. It would not work in single-threaded code because both
putandgetcould block the thread indefinitely.@mlucy's idea of using callbacks seems best to me. It is the most general. Users could easily use a queue by adding
q.putas a callback.larkost commentedon Nov 7, 2014
@AtnNn You are mistaking my suggestion: I was suggesting that if we are looking for a model that we emulate the interface of
Queue. It is not the cleanest out there, but it is one that Python programmers trying to do this sort of thing are already accustomed to. We don't really need normalgetorputsince we already have that (hence my comment aboutQueuenot having been modeled as an extension of iterables), but theget_nowait()call is essentially what you are asking for. Extending cursors this way would be a clean way of getting this model.Callbacks are not often used in Python outside a few packages and so are a poor choice.
AtnNn commentedon Nov 7, 2014
Sorry, I did misunderstand your proposal. It would be nice to have
next_nowait, but I don't see how it would solve this issue. I have realised callbacks would not work either without an event loop.asyncioseems to be Python's answer to this issue, as discussed in #2622. We could bite the bullet and rewrite the driver to use that model.The Ruby driver also has the limitation described in this issue.
larkost commentedon Nov 7, 2014
asynciois new in Python3.4, and is subject to much change even there. Additionally it is an event-loop based system, so would be a very different model than our current driver. If that was the direction we want to go then we would be better off going with something that is more portable liketwisted(call back based) orgreenlets(would non standard package with monkey-patching). But either of those would be almost complete driver re-writes, and so would be better to have a either community or secondary drivers.But I don't see how the
get_nowait()method does not solve the problem blocking operations. To illustrate...Current version:
So the problem illustrated is that with the next/iterator model you will not see any change in
buntil you see one ina. This is of course a problem.With a
get_nowait():Here you will see a value from either, both, or neither depending on what is already available. In a real example you would wrap this in a
whileblock with some appropriate sleeps depending on where you got data.Now I believe that there are more problems lurking below the surface in how the server actually fulfills these requests, but am a little foggy about the conversation I had with @mlucy about that a while ago.
larkost commentedon Nov 7, 2014
Oh, and to add the example for the second possible solution that I posted earlier:
Important to note here is that even though they emit
StopIterationerrors (how iterators signal they are done), bothfeedAandfeedBwould still be capable of returning further results. This would be very non-standard behavior in Python, but it is an elegant solution.Edit: the major problem here is how do we signal when a changefeed is satisfied? (e.g.: one with a
limit)AtnNn commentedon Nov 7, 2014
A
get_nowaitoperation would still have to block while asking the server if there are any more available documents. I would prefer a solution that does not block.That would be ugly and would work in some cases, but when reading from a real-time feed, any form of sleep is inappropriate.
danielmewes commentedon Nov 7, 2014
I think
get_nowait()solves the problem. We could add aconn.wait_any()function on the connection, that would wait until there's new data for any of the active queries to avoid the constant sleep in the outer loop.One thing that might make callbacks slightly nicer is that code that uses change feeds can be decoupled better.
With the
get_nowait(), you need some kind of central place that repeatedly calls everything that might have a change feed open. With a callback, the connection is the only thing in your application that has to keep track of who currently has changefeeds running.So I imagine that callbacks could be easier to use if you have many different components in your application accessing RethinkDB, and not one place that coordinates all of them.
Could be support both interfaces? A
Queue-like one, and a callback one?danielmewes commentedon Nov 7, 2014
(...we could internally implement the
Queueinterface on top of the callback interface presumably)VeXocide commentedon Nov 7, 2014
Solving this through
StopIterationis not only non-standard but it would prevent these iterators being used in conjunction with theitertoolsmodule for example, we really shouldn't go down that path.larkost commentedon Nov 7, 2014
I don't think that that is a requirement. Rather
get_nowait()would only check if there were already received responses. In this model we would have outstanding requests for any active changefeeds and either use any call (e.g.:nextorget_nowait) as a chance to do general housekeeping work such as processing arrived packets and then do the check in the cursor. Or we separate that into its own background thread simplifying some parts and complicating others. But in either case it would still solve the presented problem.larkost commentedon Nov 7, 2014
@danielmewes I am not sure what you mean. So far I think that the
Queue-like idea is completely orthogonal to callbacks, which would require a runloop.Edit: sorry... missed one message.
77 remaining items
gchpaco commentedon Feb 5, 2015
If we rebase the Python driver onto
asynciothenrun,connectand probably cursors would become something like this:and similarly for the others. That is, they will automatically play nice with async stuff under the scenes, so you don't need to use
arunandaconnectand the like unless actually necessary.timmaxw commentedon Feb 5, 2015
If we implement
query.run()as @gchpaco suggested, we wouldn't even need aconn.loop_forever()method. Instead, it would just be:deontologician commentedon Feb 5, 2015
Count me onboard for the callbacks for changefeeds option. I'm concerned about making the basic "just get started" version significantly different than the code you'd need to write real applications in. It seems the
r.joinsyntax does force the user to write a select loop which is annoying and handled by all of these async libraries already.Ultimately, the users and the driver will be gravitating towards asyncio over time, so it makes sense to align the driver with that api vs. inventing something specific for our needs. A lot of time and thought went into the asyncio interface, and we probably shouldn't waste time coming up with something unique.
gchpaco commentedon Feb 5, 2015
I would make
conn.loop_forever()exist, but be a direct call intorun_forever()on the associated event loop. Easier to explain to new users, does the same thing.coffeemug commentedon Feb 6, 2015
Friendly reminder -- you can run
mergeon streams (and feeds) today. So the above code could be written like this:Or better yet:
WRT to the API:
asyncioin v1.danielmewes commentedon Feb 10, 2015
In an offline discussion with @larkost , @coffeemug , @timmaxw and @mlucy , we decided to provide an event-loop integration already in 2.0.
Apart from #3529 , this is going to be the only way to read from multiple changefeeds at the same time in Python.
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.An open question was which frameworks we should support first.
Twisted seems to be very widely used and easily available, so I think we should definitely support that.
As a second one @gchpaco suggested asyncio for Python >= 3.4 and the similar Trollius for older versions of Python. It probably makes sense to support that second.
danielmewes commentedon Feb 10, 2015
@gchpaco said he would start looking into a Twisted integration.
danielmewes commentedon Feb 16, 2015
Marking as settled. We can discuss details in follow-up issues for specific async / event libraries.
danielmewes commentedon Feb 27, 2015
Closing this.
It will be solved by either of
#2622
#3529
#3785
(and further ports we might do in the future)