from twisted.internet import reactor from twisted.internet import task
(snip)
Bon, il semblerait que l'on ne puisse appeler les méthodes start() et stop() qu'une seule fois.
Si quelqu'un pouvait m'indiquer une autre méthode pour arriver au résultat en utilisant twisted, cela m'aiderais bien.
Honnêtement, je pense que tu aura de meilleures réponses sur la ml de twisted.
Amaury Forgeot d'Arc
Bonsoir,
J'ai un petit problème avec le reacteur de Twisted et j'aimerais bien comprendre pour quel raison mon programme fige.
Voici le code qui devrait boucler ad eternam :
from twisted.internet import reactor from twisted.internet import task
class reactorProb(object): def __init__(self): self.count = 5 self._callback = task.LoopingCall(self.Heartbeat) self._callback.start(1, now = False) def Heartbeat(self): print "Top" self.count -= 1 if (self.count == 0): print "Exit" self._callback.stop() del self._callback reactor.stop()
while (True): rp = reactorProb() print "Enter into reactor.run()" reactor.run() print "Leave from reactor.run()"
J'obtiens ceci :
Enter into reactor.run() Top Top Top Top Top Exit Leave from reactor.run() Enter into reactor.run() Top Top Top Top Top Exit
Donc je démarre une première fois le réacteur, je l'arrête puis je redémarre et je fige au lieu de l'arrêter le réacteur.
Quelqu'un pourrait m'expliquer le pourquoi du comment ?
Comme il a été dit, un réacteur ne fonctionne qu'une seule fois (peut-être faudrait-il l'appeler 'fusée', ou 'allumette').
Souvent en twisted, une tâche que l'on attend crée un Deferred, et le déclenche quand elle a fini. Par chance (!) c'est ce que fait un LoopingCall ! Pour reprendre ton exemple:
from twisted.internet import reactor from twisted.internet import task
class reactorProb(object): def __init__(self): self.count = 5 self._callback = task.LoopingCall(self.Heartbeat) self._callback.start(1, now = False) def Heartbeat(self): print "Top" self.count -= 1 if self.count == 0: print "Exit" self._callback.stop() del self._callback
def startProb(result=None): print "starting task" rp = reactorProb() rp._callback.deferred.addCallback(startProb)
startProb() reactor.run()
C'est l'appel à self._callback.stop() qui déclenche le deferred. Et comme on a attaché la bonne callback... on recommence! Sans toucher au réacteur.
J'ai un petit problème avec le reacteur de Twisted et j'aimerais bien
comprendre pour quel raison mon programme fige.
Voici le code qui devrait boucler ad eternam :
from twisted.internet import reactor
from twisted.internet import task
class reactorProb(object):
def __init__(self):
self.count = 5
self._callback = task.LoopingCall(self.Heartbeat)
self._callback.start(1, now = False)
def Heartbeat(self):
print "Top"
self.count -= 1
if (self.count == 0):
print "Exit"
self._callback.stop()
del self._callback
reactor.stop()
while (True):
rp = reactorProb()
print "Enter into reactor.run()"
reactor.run()
print "Leave from reactor.run()"
J'obtiens ceci :
Enter into reactor.run()
Top
Top
Top
Top
Top
Exit
Leave from reactor.run()
Enter into reactor.run()
Top
Top
Top
Top
Top
Exit
Donc je démarre une première fois le réacteur, je l'arrête puis je
redémarre et je fige au lieu de l'arrêter le réacteur.
Quelqu'un pourrait m'expliquer le pourquoi du comment ?
Comme il a été dit, un réacteur ne fonctionne qu'une seule fois
(peut-être faudrait-il l'appeler 'fusée', ou 'allumette').
Souvent en twisted, une tâche que l'on attend crée un Deferred, et le
déclenche quand elle a fini. Par chance (!) c'est ce que fait un
LoopingCall ! Pour reprendre ton exemple:
from twisted.internet import reactor
from twisted.internet import task
class reactorProb(object):
def __init__(self):
self.count = 5
self._callback = task.LoopingCall(self.Heartbeat)
self._callback.start(1, now = False)
def Heartbeat(self):
print "Top"
self.count -= 1
if self.count == 0:
print "Exit"
self._callback.stop()
del self._callback
def startProb(result=None):
print "starting task"
rp = reactorProb()
rp._callback.deferred.addCallback(startProb)
startProb()
reactor.run()
C'est l'appel à self._callback.stop() qui déclenche le deferred. Et
comme on a attaché la bonne callback... on recommence! Sans toucher au
réacteur.
J'ai un petit problème avec le reacteur de Twisted et j'aimerais bien comprendre pour quel raison mon programme fige.
Voici le code qui devrait boucler ad eternam :
from twisted.internet import reactor from twisted.internet import task
class reactorProb(object): def __init__(self): self.count = 5 self._callback = task.LoopingCall(self.Heartbeat) self._callback.start(1, now = False) def Heartbeat(self): print "Top" self.count -= 1 if (self.count == 0): print "Exit" self._callback.stop() del self._callback reactor.stop()
while (True): rp = reactorProb() print "Enter into reactor.run()" reactor.run() print "Leave from reactor.run()"
J'obtiens ceci :
Enter into reactor.run() Top Top Top Top Top Exit Leave from reactor.run() Enter into reactor.run() Top Top Top Top Top Exit
Donc je démarre une première fois le réacteur, je l'arrête puis je redémarre et je fige au lieu de l'arrêter le réacteur.
Quelqu'un pourrait m'expliquer le pourquoi du comment ?
Comme il a été dit, un réacteur ne fonctionne qu'une seule fois (peut-être faudrait-il l'appeler 'fusée', ou 'allumette').
Souvent en twisted, une tâche que l'on attend crée un Deferred, et le déclenche quand elle a fini. Par chance (!) c'est ce que fait un LoopingCall ! Pour reprendre ton exemple:
from twisted.internet import reactor from twisted.internet import task
class reactorProb(object): def __init__(self): self.count = 5 self._callback = task.LoopingCall(self.Heartbeat) self._callback.start(1, now = False) def Heartbeat(self): print "Top" self.count -= 1 if self.count == 0: print "Exit" self._callback.stop() del self._callback
def startProb(result=None): print "starting task" rp = reactorProb() rp._callback.deferred.addCallback(startProb)
startProb() reactor.run()
C'est l'appel à self._callback.stop() qui déclenche le deferred. Et comme on a attaché la bonne callback... on recommence! Sans toucher au réacteur.