やちです。

提示していただいたcodeでこちらの思い通りの動作になりました。
実行context?をnodeに戻さないといけないのは、考えてみればあたりまえなんですけど、気がつきませんでした。

ありがとうございました。

2012/4/26 Shigeki Ohtsu <oh...@iij.ad.jp>:
> 大津です。
>
> while(true) {} が終了しないので次のイベントループに回らないからだと思い
> ます。
> (正確には tp.js の読み込みで1回分イベントループが回っていますが)
>
> while(true) で回すのではなく process.nextTick() で再帰的にカウンター
> を回せば、おそらくシグナルウォッチャーのコールバックを受けることが
> できるはずです。
>
> 以下を試してみたらいかがでしょうか?
>
> -------------------------------------------------------
> process.on('SIGUSR1', function (){
>
> console.log("usr1.");
> process.exit(100);
> });
> process.on('SIGINT', function (){
> console.log("interrupted.");
> process.exit(200);
> });
> process.on('exit', function (){
> console.log("exit.");
> });
>
> var c = 0;
> function output() {
> process.nextTick(function() {
> console.log("foo" + c);
> c++;
> // if (c == 10000) {
> // process.exit(10);
> // }
> output();
> });
> }
> output();
>
> -------------------------------------------------------
>
>
> (2012/04/26 23:23), Taro YACHI wrote:
>> やちといいます。
>>
>> 環境:
>> FreeBSD 8.2R p3
>> node v0.6.14
>>
>> SIGINTとかのsignalをうけとりたいと思い、下記のようなcodeを書きました。
>>
>> tp.js
>> -------------------------------------------------------
>> process.on('SIGUSR1', function (){
>>       console.log("usr1.");
>>       process.exit(100);
>> });
>> process.on('SIGINT', function (){
>>       console.log("interrupted.");
>>       process.exit(200);
>> });
>> process.on('exit', function (){
>>       console.log("exit.");
>> });
>>
>> var c = 0;
>> while(true){
>>       console.log("foo" + c);
>>       c++;
>> //    if (c == 10000) {
>> //            process.exit(10);
>> //    }
>> }
>> -------------------------------------------------------
>>
>> で、$ node tp.js として、実行。他のterminalから$ killall -USR1 node としても、割り込みにより
>> console.logでの文言を出力したり、process.exit()によって止めたり出来ません。また、nodeを実行したterminalで
>> CTRL-Cをしても止まりません。
>> SIGINTのprocess.on()を削ると、CTRL-Cで止まるので、trapは出来ているような気がしています。が、
>> console.llogしている文言はいずれも出力されません。
>>
>> なにか、指摘をお願いします。
>>
>

メールによる返信