[Twisted-Python] does setTcpNoDelay() not work?
Hello, I'm needing to send small packets to update a progress bar. My update msgs were all being sent in big packets, so I tried setting setTcpNoDelay() in connectionMade() but it doesn't change anything, is it broken? Thank you, Gabriel
Gabriel Rossetti wrote:
Hello,
I'm needing to send small packets to update a progress bar. My update msgs were all being sent in big packets, so I tried setting setTcpNoDelay() in connectionMade() but it doesn't change anything, is it broken?
Thank you, Gabriel
I an using code that I wrote using threads.deferToThread(), I wondered if it was blocking the reactor maybe, so I tried just using a plain deferToThread() call. Here's the code (without all the protocol stuff, it's executed when data come in. def loop(self): import time m = utils.createMessage("toto", "hello") while(True): print "Sending %s" % m.toXml() self.sendMessage(m) print "Sent!" time.sleep(2) threads.deferToThread(self.loop) the TCP no delay option is turned on like I said before, the weird thing is that I have the same thing, all my msgs get sent in big blocks. Either the TCP no delay doesn't work, or the reactor is getting tied up. I don' t get it since I'm running the blocking code in a thread, the reactor should have nothing to do. Thank you, Gabriel P.S. I know that I'm not returning a deferred, I am in my real code though, but this is just "debug" code.
On Mon, 11 Aug 2008 19:13:56 +0200, Gabriel Rossetti
Gabriel Rossetti wrote:
Hello,
I'm needing to send small packets to update a progress bar. My update msgs were all being sent in big packets, so I tried setting setTcpNoDelay() in connectionMade() but it doesn't change anything, is it broken?
Thank you, Gabriel I an using code that I wrote using threads.deferToThread(), I wondered if it was blocking the reactor maybe, so I tried just using a plain deferToThread() call. Here's the code (without all the protocol stuff, it's executed when data come in.
def loop(self): import time m = utils.createMessage("toto", "hello") while(True): print "Sending %s" % m.toXml() self.sendMessage(m) print "Sent!" time.sleep(2)
threads.deferToThread(self.loop)
the TCP no delay option is turned on like I said before, the weird thing is that I have the same thing, all my msgs get sent in big blocks. Either the TCP no delay doesn't work, or the reactor is getting tied up. I don' t get it since I'm running the blocking code in a thread, the reactor should have nothing to do.
Since you didn't include the definition of `sendMessage´, it's hard to say what's really going on here. However, my guess is that you're calling the transport.write method in a non-reactor thread, which is not allowed. If this isn't the case, including a full, minimal example in your next message would be a good idea. :) Jean-Paul
Jean-Paul Calderone wrote:
On Mon, 11 Aug 2008 19:13:56 +0200, Gabriel Rossetti
wrote: Hello,
I'm needing to send small packets to update a progress bar. My update msgs were all being sent in big packets, so I tried setting setTcpNoDelay() in connectionMade() but it doesn't change anything, is it broken?
Thank you, Gabriel I an using code that I wrote using threads.deferToThread(), I wondered if it was blocking the reactor maybe, so I tried just using a plain deferToThread() call. Here's the code (without all the
Gabriel Rossetti wrote: protocol stuff, it's executed when data come in.
def loop(self): import time m = utils.createMessage("toto", "hello") while(True): print "Sending %s" % m.toXml() self.sendMessage(m) print "Sent!" time.sleep(2)
threads.deferToThread(self.loop)
the TCP no delay option is turned on like I said before, the weird thing is that I have the same thing, all my msgs get sent in big blocks. Either the TCP no delay doesn't work, or the reactor is getting tied up. I don' t get it since I'm running the blocking code in a thread, the reactor should have nothing to do.
Since you didn't include the definition of `sendMessage´, it's hard to say what's really going on here. However, my guess is that you're calling the transport.write method in a non-reactor thread, which is not allowed.
If this isn't the case, including a full, minimal example in your next message would be a good idea. :)
Jean-Paul
Yes, self.sendMessage() calls transport.write. So there's no way of sending msgs from a non-reactor thread, even if it has a reference to the protocol? Gabriel
On Mon, 11 Aug 2008 20:18:43 +0200, Gabriel Rossetti
Jean-Paul Calderone wrote:
On Mon, 11 Aug 2008 19:13:56 +0200, Gabriel Rossetti
wrote: Gabriel Rossetti wrote:
Hello,
I'm needing to send small packets to update a progress bar. My update msgs were all being sent in big packets, so I tried setting setTcpNoDelay() in connectionMade() but it doesn't change anything, is it broken?
Thank you, Gabriel I an using code that I wrote using threads.deferToThread(), I wondered if it was blocking the reactor maybe, so I tried just using a plain deferToThread() call. Here's the code (without all the protocol stuff, it's executed when data come in.
def loop(self): import time m = utils.createMessage("toto", "hello") while(True): print "Sending %s" % m.toXml() self.sendMessage(m) print "Sent!" time.sleep(2)
threads.deferToThread(self.loop)
the TCP no delay option is turned on like I said before, the weird thing is that I have the same thing, all my msgs get sent in big blocks. Either the TCP no delay doesn't work, or the reactor is getting tied up. I don' t get it since I'm running the blocking code in a thread, the reactor should have nothing to do.
Since you didn't include the definition of `sendMessage´, it's hard to say what's really going on here. However, my guess is that you're calling the transport.write method in a non-reactor thread, which is not allowed.
If this isn't the case, including a full, minimal example in your next message would be a good idea. :)
Jean-Paul Yes, self.sendMessage() calls transport.write. So there's no way of sending msgs from a non-reactor thread, even if it has a reference to the protocol?
There is a way. The way is to use reactor.callFromThread to schedule a function to be called in the reactor thread. eg, reactor.callFromThread(self.sendMessage) Jean-Paul
Jean-Paul Calderone wrote:
On Mon, 11 Aug 2008 20:18:43 +0200, Gabriel Rossetti
wrote: On Mon, 11 Aug 2008 19:13:56 +0200, Gabriel Rossetti
wrote: Hello,
I'm needing to send small packets to update a progress bar. My update msgs were all being sent in big packets, so I tried setting setTcpNoDelay() in connectionMade() but it doesn't change anything, is it broken?
Thank you, Gabriel I an using code that I wrote using threads.deferToThread(), I wondered if it was blocking the reactor maybe, so I tried just using a plain deferToThread() call. Here's the code (without all
Gabriel Rossetti wrote: the protocol stuff, it's executed when data come in.
def loop(self): import time m = utils.createMessage("toto", "hello") while(True): print "Sending %s" % m.toXml() self.sendMessage(m) print "Sent!" time.sleep(2)
threads.deferToThread(self.loop)
the TCP no delay option is turned on like I said before, the weird thing is that I have the same thing, all my msgs get sent in big blocks. Either the TCP no delay doesn't work, or the reactor is getting tied up. I don' t get it since I'm running the blocking code in a thread, the reactor should have nothing to do.
Since you didn't include the definition of `sendMessage´, it's hard to say what's really going on here. However, my guess is that you're calling the transport.write method in a non-reactor thread, which is not allowed.
If this isn't the case, including a full, minimal example in your next message would be a good idea. :)
Jean-Paul Yes, self.sendMessage() calls transport.write. So there's no way of sending msgs from a non-reactor thread, even if it has a reference to
Jean-Paul Calderone wrote: the protocol?
There is a way. The way is to use reactor.callFromThread to schedule a function to be called in the reactor thread.
eg, reactor.callFromThread(self.sendMessage)
Jean-Paul
Thank you Jean-Paul & Christopher, I hadn't realized that. Now I understand better why in deferToThread() you use reactor.callFromThread() to add callbacks/errbacks. So if I understand, anything that uses twisted, that I'd need to call from a thread, needs to be called with reactor.callFromThread(). Gabriel
On Mon, Aug 11, 2008 at 2:18 PM, Gabriel Rossetti
Jean-Paul Calderone wrote:
Since you didn't include the definition of `sendMessage´, it's hard to say what's really going on here. However, my guess is that you're calling the transport.write method in a non-reactor thread, which is not allowed.
If this isn't the case, including a full, minimal example in your next message would be a good idea. :)
Jean-Paul
Yes, self.sendMessage() calls transport.write. So there's no way of sending msgs from a non-reactor thread, even if it has a reference to the protocol?
use reactor.callFromThread, which takes as an argument a function to call in the reactor thread. That function can use Twisted APIs like transport.write. Most Twisted APIs are not thread-safe, and cannot be called from a non-reactor thread. -- Christopher Armstrong International Man of Twistery http://radix.twistedmatrix.com/ http://twistedmatrix.com/ http://canonical.com/
participants (3)
-
Christopher Armstrong
-
Gabriel Rossetti
-
Jean-Paul Calderone