Syntactic Confectionery Delight | |
PerlMonks |
Re: Trying to exit an HTTP::Daemon based on JSON::RPC::Server::Daemonby sundialsvc4 (Abbot) |
on Oct 10, 2010 at 14:31 UTC ( [id://864482]=note: print w/replies, xml ) | Need Help?? |
First of all, please edit any code that you copy so that it includes only the minimal amount of material necessary to illustrate your point. (If you copied it from another, well-known source such as a CPAN module, please cite the module ... we can find it ourselves if we need to). You can see from the referenced code that the receipt of a signal ought to merely cause a flag (e.g. $exit_now) to become true, and to wake up the process or thread to wake up if it is sleeping. This causes it to promptly exit from the while loop that it is parked in. The thread should then clean up its own affairs neatly, and exit normally. (If necessary, it can issue some signal or send some message to its parent before it dies.) Also notice that “it is best to wake somebody up using an alarm clock, not a loaded pistol.” Certain signals, such as KILL, have specific meaning to the operating system. You do not want the OS to be the one who’s reacting to the signal that you give. You want the recipient to kill itself, not to “be killed.” Finally, you want to be certain that the termination signal is handled “gracefully and cleanly,” and always at an appropriate point in the recipient’s normal business day. (If you shoot somebody in the head, you’re going to leave blood-n-guts on the floor ... and that is, y’know, very hard to clean.) If you are, say, already using message-queues to talk between a set of processes or threads, simply design one of those messages to be “the mark of death.” Upon receipt of this special message, the thread breaks out of its message-handling loop, closes its side of the message-queue etc., and dies quietly. The sender of the message knows that any messages that might have been in the queue ahead of the “mark of death” message, will have been processed normally, as well. The “mark of death” message is an upleasant surprise, but like all other messages it is processed synchronously. As a final matter of principle, you really want this sort of thing to only be happening when the program is closing down ... not as a regular part of the program’s daily routine. If the number of threads is kept sensibly small, any one of them can wait, even for many hours, with almost no overhead at all. A single thread might handle thousands of units-of-work during the course of its lifetime.
In Section
Seekers of Perl Wisdom
|
|