[Twisted-Python] reactor.stop stops entire twistd

Hi, I have the following codes: site = server.Site(Core()) application = service.Application('server') sc = service.IServiceCollection(application) q = internet.TCPServer(8081, site) q.setServiceParent(sc) # twistd -y server.tac The core class uses methods that uses getProcessOutput, reactor.run aad reactor.stop. However, the whole application gets shutdown because of the reactor.stop. How do I avoid having my entire application getting stopped? Thanks, Liming

On Wed, 28 Jul 2004 16:41:49 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
Don't call reactor.stop()... "Doctor, doctor, it hurts when I do this!" ;-) -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

On Wed, 28 Jul 2004 17:07:36 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
Ok. then how do I aviod using reactor.start() and reactor.stop() for getProcessOutut?
Well, since you're using twistd, the reactor is already running, so you don't need to call reactor.run(). And you shouldn't be calling reactor.stop(), because you said you didn't want to stop the reactor. So, just don't call them. But the fact that I need to say something so obvious makes me think I'm not grasping the whole picture. If my explanation so far is insufficient, can you show more code? -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

Christopher, We have the following class that is instantiated within the sever code that is using twistd to issue system calls. class System: result = None status = None cmd = '' def __init__(self, cmd, args=[]): self.cmd = cmd self.args = args def ok(self, result): self.result = result self.status = 0 reactor.stop() def err(self, err): self.result = err.getErrorMessage() self.status = 1 reactor.stop() def run(self): d = utils.getProcessOutput(self.cmd, self.args, os.environ) d.addCallback(self.ok) d.addErrback(self.err) reactor.run() def getResult(self): if self.status == None: self.status = 0 if self.result[-1:] == '\n': self.result = self.result[:-1] return self.status, self.result Since the reactor is already running, how do I actually use Deferred? Many thanks, Liming Christopher Armstrong wrote:

On Wed, 28 Jul 2004 17:29:24 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
It looks like you just need to take out those calls to reactor.stop() and reactor.run(). -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

On Wed, 28 Jul 2004 18:40:52 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
No. Your call to getProcessOutput creates the Deferred object and returns it to you. getProcessOutput starts the program and sets it up so the Deferred will be triggered once the output is done. Your function attaches callback(s) to the Deferred. When your function returns, control is returned to the reactor. Eventually, the process will finish, causing the Deferred to be triggered (which happens in code that's connected with getProcessOutput), causing your callback to fire with the output of the process. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

Liming, You may want to read the fine documentation howto sections... http://www.twistedmatrix.com/documents/current/howto/ I think if you put a little time into reading, all your questions will be answered. Anthony Tsai Li Ming wrote:
-- ____________________________________________________________ *Anthony Tarlano Researcher - Future Networking Laboratory* *DoCoMo Communications Laboratories Europe GmbH** * *Landsberger Strasse 308-312 * *80687 Munich Germany* *Tel:** +**49 89 56 824 212* *Cell: ** +**49 177 341 3758* *Fax:** ** ** ** **+**49 89 56 824 300* mailto:tarlano@docomolab-euro.com http://www.docomolab-euro.com/

On Wed, 2004-07-28 at 09:41, Tsai Li Ming wrote:
Calling reactor.stop() is supposed to shutdown the application (see below) so please could you explain what you actually wanted to stop by calling reactor.stop(). In the meantime, here's a couple of points to consider ... 1. The reactor is the event loop for the process and, once running, *everything* happens because of an event. If you stop the reactor then you are explicitly telling the reactor to stop processing events and end the application. 2. twistd manages the creation and activation of the reactor for you (as well as other useful stuff such as logging). You should not call reactor.run() in your application. Cheers, Matt -- __ / \__ Matt Goodall, Pollenation Internet Ltd \__/ \ w: http://www.pollenation.net __/ \__/ e: matt@pollenation.net / \__/ \ t: +44 (0)113 2252500 \__/ \__/ / \ Any views expressed are my own and do not necessarily \__/ reflect the views of my employer.

On Wed, 28 Jul 2004 16:41:49 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
Don't call reactor.stop()... "Doctor, doctor, it hurts when I do this!" ;-) -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

On Wed, 28 Jul 2004 17:07:36 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
Ok. then how do I aviod using reactor.start() and reactor.stop() for getProcessOutut?
Well, since you're using twistd, the reactor is already running, so you don't need to call reactor.run(). And you shouldn't be calling reactor.stop(), because you said you didn't want to stop the reactor. So, just don't call them. But the fact that I need to say something so obvious makes me think I'm not grasping the whole picture. If my explanation so far is insufficient, can you show more code? -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

Christopher, We have the following class that is instantiated within the sever code that is using twistd to issue system calls. class System: result = None status = None cmd = '' def __init__(self, cmd, args=[]): self.cmd = cmd self.args = args def ok(self, result): self.result = result self.status = 0 reactor.stop() def err(self, err): self.result = err.getErrorMessage() self.status = 1 reactor.stop() def run(self): d = utils.getProcessOutput(self.cmd, self.args, os.environ) d.addCallback(self.ok) d.addErrback(self.err) reactor.run() def getResult(self): if self.status == None: self.status = 0 if self.result[-1:] == '\n': self.result = self.result[:-1] return self.status, self.result Since the reactor is already running, how do I actually use Deferred? Many thanks, Liming Christopher Armstrong wrote:

On Wed, 28 Jul 2004 17:29:24 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
It looks like you just need to take out those calls to reactor.stop() and reactor.run(). -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

On Wed, 28 Jul 2004 18:40:52 +0800, Tsai Li Ming <mailinglist@ltsai.com> wrote:
No. Your call to getProcessOutput creates the Deferred object and returns it to you. getProcessOutput starts the program and sets it up so the Deferred will be triggered once the output is done. Your function attaches callback(s) to the Deferred. When your function returns, control is returned to the reactor. Eventually, the process will finish, causing the Deferred to be triggered (which happens in code that's connected with getProcessOutput), causing your callback to fire with the output of the process. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://radix.twistedmatrix.com

Liming, You may want to read the fine documentation howto sections... http://www.twistedmatrix.com/documents/current/howto/ I think if you put a little time into reading, all your questions will be answered. Anthony Tsai Li Ming wrote:
-- ____________________________________________________________ *Anthony Tarlano Researcher - Future Networking Laboratory* *DoCoMo Communications Laboratories Europe GmbH** * *Landsberger Strasse 308-312 * *80687 Munich Germany* *Tel:** +**49 89 56 824 212* *Cell: ** +**49 177 341 3758* *Fax:** ** ** ** **+**49 89 56 824 300* mailto:tarlano@docomolab-euro.com http://www.docomolab-euro.com/

On Wed, 2004-07-28 at 09:41, Tsai Li Ming wrote:
Calling reactor.stop() is supposed to shutdown the application (see below) so please could you explain what you actually wanted to stop by calling reactor.stop(). In the meantime, here's a couple of points to consider ... 1. The reactor is the event loop for the process and, once running, *everything* happens because of an event. If you stop the reactor then you are explicitly telling the reactor to stop processing events and end the application. 2. twistd manages the creation and activation of the reactor for you (as well as other useful stuff such as logging). You should not call reactor.run() in your application. Cheers, Matt -- __ / \__ Matt Goodall, Pollenation Internet Ltd \__/ \ w: http://www.pollenation.net __/ \__/ e: matt@pollenation.net / \__/ \ t: +44 (0)113 2252500 \__/ \__/ / \ Any views expressed are my own and do not necessarily \__/ reflect the views of my employer.
participants (4)
-
Anthony Tarlano
-
Christopher Armstrong
-
Matt Goodall
-
Tsai Li Ming