[Twisted-Python] producer / consumer question
Hi, I have yet another question... a producer has to learn about it's consumer somehow, otherwise it would not know where to write it's data to. But the proposed interface is exactly vice versa: the IConsumer interface has a registerProducer() method. Now, that would be fine, if that did all the setup between a producer and a consumer. But AFACIS in the twisted code, registerProducer() does not link itself with the producer. Instead, the producers I've inspected (i.e. FileSender) require an explicit initialization with a given consumer. Why is that? What's the best way to associate a producer with a consumer? Is something like the following okay: def associateProducerConsumer(prod, cons): prod.consumer = cons cons.registerProducer(prod, ISeekableProducer.providedBy(prod)) Regards Markus
On Wed, 04 Jul 2007 18:13:05 +0200, Markus Schiltknecht <markus@bluegap.ch> wrote:
Hi,
I have yet another question... a producer has to learn about it's consumer somehow, otherwise it would not know where to write it's data to.
But the proposed interface is exactly vice versa: the IConsumer interface
def associateProducerConsumer(prod, cons): prod.consumer = cons cons.registerProducer(prod, ISeekableProducer.providedBy(prod))
This question is also part of what I'm asking for here: http://twistedmatrix.com/pipermail/twisted-web/2007-July/003434.html I'd like some discussion and help and guidance too :).
On 04:13 pm, markus@bluegap.ch wrote:
I have yet another question... a producer has to learn about it's consumer somehow, otherwise it would not know where to write it's data to.
Why is that? What's the best way to associate a producer with a consumer? Is something like the following okay:
This is a really deep problem, unfortunately, and Valentino has correctly identified that it's the same one I was going to talk about when responding to his twisted-web posting :). Your solution doesn't really solve anything, since neither producers nor consumers are documented to have the attributes you are assigning, nor do anything with them. Consumers are documented to have a "write" method, but the documentation is misleading. The most common type of producer - tcp.Connection - does not put data into its consumer's "write" method, it calls its protocol's dataReceived method. Many other producers are actually written specifically to call higher-level methods on something other than their consumer (such as callRemote) rather than "write". The basic use-case here is that the responsibility for producing bytes and the responsibility for throttling data production are usually separated in an application. I agree that this is not really the best way to separate them. The API grew up almost by accident, it wasn't really designed intentionally, although it has worked surprisingly well considering. In many cases it does seem that we want a high-level API for setting up pipelines of producer-to-consumer-to-producer, or (in the case of web2 streams) end-to-end producer-producer-producer sequences. While many of these use-cases are superficially similar, my own attempts to resolve this shows that there is a surprising diversity of requirements which are difficult to resolve simultaneously by "simply fixing" the existing consumer / producer mess. Keep in mind, also, that a great deal of code has been written (and will be written before everyone has upgraded to the next version of Twisted) that uses the current IConsumer/IProducer APIs, and all of that will need to keep working for the forseeable future. You can find some discussion, as well as our attempts to create something better, here: http://twistedmatrix.com/trac/ticket/1956 If you have new information, feel free to comment there, but please read all the comments and try to understand them first: there has been a *lot* of back-and-forth over this issue.
Hi, glyph@divmod.com wrote:
Keep in mind, also, that a great deal of code has been written (and will be written before everyone has upgraded to the next version of Twisted) that uses the current IConsumer/IProducer APIs, and all of that will need to keep working for the forseeable future.
Sure. I'm currently in the situation, that I need to write a stream server capable of handling various sources (database, files, http, etc...) and various formats (images, videos, etc..). And as we've already learned, it's well worth thinking about a good abstraction for such streaming tasks. The IConsumer/IProducer pair seems to be the right thing, but if the API isn't stable (AFAIR semi-stable is stated in the documentation)...
You can find some discussion, as well as our attempts to create something better, here:
http://twistedmatrix.com/trac/ticket/1956
If you have new information, feel free to comment there, but please read all the comments and try to understand them first: there has been a *lot* of back-and-forth over this issue.
Okay, thank you very much for the pointers. I'll investigate further ;-) Regards Markus
On Thu, 05 Jul 2007 09:01:00 +0200, Markus Schiltknecht <markus@bluegap.ch> wrote:
Hi,
glyph@divmod.com wrote:
Keep in mind, also, that a great deal of code has been written (and will be written before everyone has upgraded to the next version of Twisted) that uses the current IConsumer/IProducer APIs, and all of that will need to keep working for the forseeable future.
Sure. I'm currently in the situation, that I need to write a stream server capable of handling various sources (database, files, http, etc...) and various formats (images, videos, etc..). And as we've already learned, it's well worth thinking about a good abstraction for such streaming tasks. The IConsumer/IProducer pair seems to be the right thing, but if the API isn't stable (AFAIR semi-stable is stated in the documentation)...
It's stable. There isn't any documentation that indicates otherwise. Jean-Paul
On 07:01 am, markus@bluegap.ch wrote:
glyph@divmod.com wrote:
Keep in mind, also, that a great deal of code has been written (and will be written before everyone has upgraded to the next version of Twisted) that uses the current IConsumer/IProducer APIs, and all of that will need to keep working for the forseeable future.
Sure. I'm currently in the situation, (...) but if the API isn't stable (AFAIR semi-stable is stated in the documentation)...
The API is stable. There are plans for a better API that offers more functionality, but we don't even know what that looks like yet. It may well be that the old API will live on side-by-side, and the new API will just expose new features. In any event, no deprecation warnings are coming in this code for a long time.
Hi, Jean-Paul Calderone wrote:
It's stable. There isn't any documentation that indicates otherwise.
Yes, there is: the current API docs clearly indicates, that "This interface is semi-stable.", for IPullProducer as well as for IPushProducer. Not for IProducer, though. See: http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.I... http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.I... Regards Markus
participants (4)
-
glyph@divmod.com
-
Jean-Paul Calderone
-
Markus Schiltknecht
-
Valentino Volonghi aka Dialtone