m_ike: (Sphinx)

Лекция по информатике.
Лектор: ... приложения, работающие в бэкграунде, они же...
Из аудитории: Демоны!
Лектор: Господь с тобой! Демоны...
(эпиграф взят на http://bash.im/ )

У меня было два задачи:

1. Когда демон хочет послать пару имейлов, каждый имейл должен посылаться в отдельном потоке, чтобы демон мог спокойно творить свое демоническое дело, не дожидаясь, пока мыло отправится. На первый взгляд, все довольно просто:
sub SendMessageInNewProcess
{
my $NewChild=fork(); # create a child process
if ($NewChild) { return }; # return if parent, stay if child

#....... (all necessary code to send an email)

exit(0); # kill child process
}
Далее вызываем процедуру хоть десять раз. Однако не все оказалось так просто. Детки-то не дохнут, а остаются в памяти с пометкой defunct, то есть зомби. Как же их убить?

2. Хочу уметь самым простым способом запускать из веб-браузера независимый процесс. То есть браузер говорит "страница полностью загружена", а процесс все еще работает. Браузер можно вообще закрыть, но процесс на сервере будет как ни в чем ни бывало работать. Что-то никак не получалось. Если браузер мог дождаться окончания процесса, то страница грузилась и грузилась, пока процесс наконец не завершится. А если это демон, который завершиться не должен, а должен загрузиться и так и остаться, то демон-то загружался, а вот браузер пыжился, а потом выдавал уродливый таймаут. Что делать? Фактически тут целых две задачи:
2а. Запустить из браузера демон
2б. Запустить процесс, который поработает еще пару секунд (минут) после того, как браузер закончит загрузку страницы

В итоге все, конечно, разрешилось. Если речь идет о таком древнем языке, как PERL, то все твои проблемы уже у кого-нибудь лет десять назад возникали и на форумах разжеваны. В частности, почти все нюансы, относящиеся ко всему вышеперечисленному, разжевываются тут:

Improving mod_perl Sites' Performance: Part 6
http://www.perl.com/pub/2003/01/07/mod_perl.html

Не могу не процитировать наиболее живописные фрагменты. Это просто готический роман:

A zombie is a process that doesn't have a parent. When the child quits, it reports the termination to its parent. If no parent wait()s to collect the exit status of the child, then it gets "confused" and becomes a ghost process. This process can be seen as a process, but not killed.
[...]
Generally, the ps(1) utility displays these processes with the tag, and you will see the zombies counter increment when doing top(). These zombie processes can take up system resources and are generally undesirable.
[...]
The simplest solution is to ignore your dead children.
[...]
The $SIG{CHLD} = 'IGNORE'; statement protects us from zombies, as explained above. Another, more portable but slightly more expensive solution, is to use a double fork approach.
m_ike: (Sphinx)
Путешествуя по перловым сайтам и форумам, нашел поистине философский крик души. Итак, стоит ли давать ответ, если задали неправильный вопрос?

When people come into comp.lang.perl.misc asking how to do something stupid, I'm never quite sure what to do. I can just answer the question as asked, figuring that it's not my problem to tell people that they're being stupid. That's in my self-interest, because it takes less time to answer the question that way, and because someone might someday pay me to clean up after their stupidity, as happened in this instance. But if I do that, people might jump on me for being a smart aleck, which has happened at times. ("Come on, help the poor guy out; if you know what he really needs why don't you just give it to him?")

On the other hand, I could try to answer on a different level, present a better solution, and maybe slap a little education on `em. That's nice when it works, but if it doesn't it's really sad to see your hard work and good advice ignored. Also, people tend to jump on you for not answering the question. ("Who are you to be telling this guy what he should be doing? Just answer the question.")

I guess there's room for both kinds of answer. Or maybe there isn't room for either kind.

http://perl.plover.com/varvarname.html
m_ike: (Default)
Мои самые распространенные ошибки в Perl Не знающим языка Perl дальше читать не стоит... )

December 2016

S M T W T F S
    123
45678 910
11121314151617
18192021222324
25262728293031

Syndicate

RSS Atom

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 15th, 2025 11:17 pm
Powered by Dreamwidth Studios