[Twisted-Python] a problem about iocp and callInThread
Hi all I just encountered a problem about iocp reactor & callInThread. I paste the code following: from twisted.internet import iocpreactor iocpreactor.install() from twisted.internet import reactor class ThreadTest: def __init__(self,name): self.count = 0 self.max = 5 self.wait = 1 self.name = name def __call__(self): self.Execute() def Execute(self): print self.name, time.time() self.count += 1 if self.count <self.max: reactor.callLater(self.wait, reactor.callInThread, self.Execute) else: print self.name, "stop" reactor.callInThread(ThreadTest("test")) the problem is every time the print method was invoked, i noticed that the difference between current invoke and previous invoke was 2 seconds. In theory ,it should be one second. When i used the plain reactor (which means no iocpreactor), the difference was one second which is a correct value. I want to know what makes callInThread so special with iocpreactor? BTW: i used windows 7 & window s2008. Regards gelin yan
I want to know what makes callInThread so special with iocpreactor?
You're calling a reactor method from a thread; that is a bug, the reactor is not thread safe. As a result, different reactors may fail differently. Use reactor.callFromThread to call reactor methods from a thread.
On Sun, May 15, 2011 at 6:10 PM, Dfgqq Dfgqq <dynamicgl@gmail.com> wrote:
reactor.callLater(self.wait, reactor.callInThread, self.Execute)
I believe this should be: reactor.callFromThread(reactor.callLater, self.wait, reactor.callInThread, self.Execute) Execute is not being called in the reactor thread, so the only Twisted API you are allowed to use is reactor.callFromThread, which schedules a callable to run in the reactor thread and wakes up the reactor if necessary. Calling other APIs from a non-reactor thread can result in various misbehaviours such as delays in event processing, as you are observing. -- mithrandi, i Ainil en-Balandor, a faer Ambar
On 04:10 pm, dynamicgl@gmail.com wrote:
Hi all
I just encountered a problem about iocp reactor & callInThread. I paste the code following:
from twisted.internet import iocpreactor iocpreactor.install() from twisted.internet import reactor
class ThreadTest: def __init__(self,name): self.count = 0 self.max = 5 self.wait = 1 self.name = name
def __call__(self): self.Execute()
def Execute(self): print self.name, time.time() self.count += 1 if self.count <self.max: reactor.callLater(self.wait, reactor.callInThread, self.Execute) else: print self.name, "stop"
reactor.callInThread(ThreadTest("test"))
the problem is every time the print method was invoked, i noticed that the difference between current invoke and previous invoke was 2 seconds. In theory ,it should be one second. When i used the plain
You're calling reactor.callLater from a non-reactor thread. This isn't supported and will produce unpredictable behavior. Jean-Paul
reactor (which means no iocpreactor), the difference was one second which is a correct value.
I want to know what makes callInThread so special with iocpreactor?
BTW: i used windows 7 & window s2008.
Regards
gelin yan
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Hi all I modified my code as reactor.callFromThread(reactor.callLater, self.wait, reactor.callInThread, self.Execute) based on your comments. now the problem have been solved. Thanks your guys. I appreciate your help. Regards gelin yan On Mon, May 16, 2011 at 12:29 AM, <exarkun@twistedmatrix.com> wrote:
On 04:10 pm, dynamicgl@gmail.com wrote:
Hi all
I just encountered a problem about iocp reactor & callInThread. I paste the code following:
from twisted.internet import iocpreactor iocpreactor.install() from twisted.internet import reactor
class ThreadTest: def __init__(self,name): self.count = 0 self.max = 5 self.wait = 1 self.name = name
def __call__(self): self.Execute()
def Execute(self): print self.name, time.time() self.count += 1 if self.count <self.max: reactor.callLater(self.wait, reactor.callInThread, self.Execute) else: print self.name, "stop"
reactor.callInThread(ThreadTest("test"))
the problem is every time the print method was invoked, i noticed that the difference between current invoke and previous invoke was 2 seconds. In theory ,it should be one second. When i used the plain
You're calling reactor.callLater from a non-reactor thread. This isn't supported and will produce unpredictable behavior.
Jean-Paul
reactor (which means no iocpreactor), the difference was one second which is a correct value.
I want to know what makes callInThread so special with iocpreactor?
BTW: i used windows 7 & window s2008.
Regards
gelin yan
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (4)
-
Dfgqq Dfgqq -
exarkun@twistedmatrix.com -
Itamar Turner-Trauring -
Tristan Seligmann