Re: [Twisted-Python] Need Exception that will stop ther reactor within twistd
On 12:55 am, ncesar@lunix.com.ar wrote:
This is bad form, however. Particular types of exception should not stop the reactor. Code that wants the reactor to stop, should call reactor.stop. Maybe you could make a method that both calls reactor.stop() *and* raises an exception.
This is the way i'm doing it right now, but i'm starting to detect new exceptions raised within standard library (among others) code... and instead of catching all posibilities I wanted the application to quit (with the corresponding logging done).
I was wondering if twistd/the application class could have a "exit on Exception" for this type of operations.
In general this is a bad idea. There are basically 3 kinds of code you can write with Twisted: 1. Infrastructure code, which implements a protocol and provides APIs for parsing and dealing with events. This kind of code should never contain a reactor.stop at all. 2. Application code which exits in response to a particular user event. This kind of code should contain exactly 1 call to reactor.stop, in the handler for the user event that is the explicit stop. 3. Programs which do one operation, then stop themselves. This kind of code should contain exactly 1 call to reactor.stop, in the final callback/errback of the Deferred representing the operation that it is run to perform. It sounds like you are writing code of type 3. I'm guessing you're writing a program which does one thing, then shuts itself down. If that is the case, the "program" should be encapsulated by a Deferred, and there should be a final addBoth which waits for the program to finish when it exits. If you *don't* structure your program this way, then you'll discover later that sometimes, you want to run your particular operation TWICE in the lifetime of the process instead of once, and you will have to deal with dozens of calls to reactor.stop littered around your application in lots of places. Don't try to always stop the *process* when your program is complete: stop the *program* by keeping a Deferred around which represents its complete run. That way the *driver* for the program is responsible for stopping it, and when you want to aggregate multiple runs of it, you can easily use a tool like DeferredList, or do something different in the callbacks at the end.
El Sábado, 25 de Noviembre de 2006 02:39, glyph@divmod.com escribió:
I was wondering if twistd/the application class could have a "exit on Exception" for this type of operations.
In general this is a bad idea. There are basically 3 kinds of code you can write with Twisted:
1. Infrastructure code, which implements a protocol and provides APIs for parsing and dealing with events. This kind of code should never contain a reactor.stop at all. (..) 3. Programs which do one operation, then stop themselves. This kind of code should contain exactly 1 call to reactor.stop, in the final callback/errback of the Deferred representing the operation that it is run to perform.
It sounds like you are writing code of type 3.
Actually is type 1, but there are horrible (network) conditions were de application should stop.
If you *don't* structure your program this way, then you'll discover later that sometimes, you want to run your particular operation TWICE in the lifetime of the process instead of once, and you will have to deal with dozens of calls to reactor.stop littered around your application in lots of places.
Even it's not type 3, I get your point. And thats why I didn't want to write ANY reactor.stop() call, instead just raise exceptions- And have a unnified reactor.stop() inside somewhere in twistd. But it seems I need to think another solution. Right now as a workaround, reactor.stop() will be inside my exception's __init__ function ;-)
Don't try to always stop the *process* when your program is complete: stop the *program* by keeping a Deferred around which represents its complete run. That way the *driver* for the program is responsible for stopping it, and when you want to aggregate multiple runs of it, you can easily use a tool like DeferredList, or do something different in the callbacks at the end.
I couldn't understand much of this last parragraph (my English reading could
be part of the problem). I understand algorithms mucho more when are written
in python instead of English, Do you have a link to some code explaning
this?
Thanks Glyph again for your detailed mail,
--
Nicolás D. César
participants (2)
-
glyph@divmod.com
-
Nicolas D. Cesar