[Twisted-Python] does setTcpNoDelay() not work?

Gabriel Rossetti gabriel.rossetti at arimaz.com
Tue Aug 12 04:34:41 EDT 2008


Jean-Paul Calderone wrote:
> On Mon, 11 Aug 2008 20:18:43 +0200, Gabriel Rossetti 
> <gabriel.rossetti at arimaz.com> wrote:
>> Jean-Paul Calderone wrote:
>>> On Mon, 11 Aug 2008 19:13:56 +0200, Gabriel Rossetti 
>>> <gabriel.rossetti at arimaz.com> 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
>
>
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




More information about the Twisted-Python mailing list