Changes between Version 12 and Version 13 of Dev/Perl


Ignore:
Timestamp:
Jan 12, 2011 2:49:22 PM (13 years ago)
Author:
mitty
Comment:

moved to example

Legend:

Unmodified
Added
Removed
Modified
  • Dev/Perl

    v12 v13  
    2323 * https://twitter.com/__gfx__/status/16314985630146560 
    2424  * PerlでDESTROYが誤って二回呼ばれてしまうコード例(by kazuho) https://gist.github.com/747040 
    25  
    26  = fork child process on CGI = 
    27  * [http://www.bioinfo.jp/tips.html Tips (CGI, Perl, Unix and etc.)] 
    28  * [http://www.tohoho-web.com/lng/199910/99100140.htm CGI-Perlのforkで子供を置き去りにするには?] 
    29  * [http://www.stackasterisk.jp/tech/program/perl04_02.jsp Perl第4回:PerlTips(バックグラウンドで処理を実行)] 
    30  
    31  * sample code 
    32    * Apache2 mpm-worker + Perl 5.10 on x64 
    33 {{{ 
    34 #!perl 
    35 #! /usr/bin/perl -w 
    36  
    37 my $pid = fork; 
    38 if (! defined $pid) { 
    39     # cannot fork 
    40     die("cannot fork: $!"); 
    41 } 
    42  
    43 if ($pid) { 
    44     # parent process 
    45     print "Content-type: text/html;\n\n"; 
    46      
    47     # output something 
    48     print "parent: $$ / child: $pid\n"; 
    49      
    50     close(STDIN); 
    51     close(STDOUT); 
    52     exit; 
    53 } 
    54 else { 
    55     # child proccess 
    56     close(STDIN); 
    57     close(STDOUT); 
    58      
    59     # long long proccess ... 
    60     sleep(60); 
    61 } 
    62 }}} 
    63    * Apache2 mpm-prefork + Perl 5.10 on x64 
    64 {{{ 
    65 #!perl 
    66 #! /usr/bin/perl -w 
    67  
    68 my $pid = fork; 
    69 if (! defined $pid) { 
    70     # cannot fork 
    71     die("cannot fork: $!"); 
    72 } 
    73  
    74 if ($pid) { 
    75     # parent process 
    76     print "Content-type: text/html;\n\n"; 
    77  
    78     print "parent: $$ / child: $pid\n"; 
    79 } 
    80 else { 
    81     # child proccess 
    82     close(STDOUT); 
    83  
    84     sleep(60); 
    85 } 
    86 }}} 
    87    * 子プロセスでclose(STDOUT)が必要とだけ書かれている文献が多いが、Apache2 mpm-worker環境では親子ともSTDINも閉じないと親プロセスは終了しなかった。 
    88    * しかし、テストのため一度mpm-preforkに切り替えた後、再度mpm-workerに戻した後は何故かpreforkと同じ挙動(STDINを閉じなくてもOK)を示すようになった。 
    89      * apache2 -V 
    90 {{{ 
    91 Server version: Apache/2.2.14 (Ubuntu) 
    92 Server built:   Apr 13 2010 20:22:19 
    93 Server's Module Magic Number: 20051115:23 
    94 Server loaded:  APR 1.3.8, APR-Util 1.3.9 
    95 Compiled using: APR 1.3.8, APR-Util 1.3.9 
    96 Architecture:   64-bit 
    97 Server MPM:     Worker 
    98   threaded:     yes (fixed thread count) 
    99     forked:     yes (variable process count) 
    100 }}} 
    101    * Apache2 mpm-preforkでは子プロセスでclose(STDOUT)するだけで良かった。 
    102      * apache2 -V 
    103 {{{ 
    104 Server version: Apache/2.2.14 (Ubuntu) 
    105 Server built:   Apr 13 2010 20:21:26 
    106 Server's Module Magic Number: 20051115:23 
    107 Server loaded:  APR 1.3.8, APR-Util 1.3.9 
    108 Compiled using: APR 1.3.8, APR-Util 1.3.9 
    109 Architecture:   64-bit 
    110 Server MPM:     Prefork 
    111   threaded:     no 
    112     forked:     yes (variable process count) 
    113 }}} 
    114    * 親プロセスについてはSTDIN/STDOUTを閉じた時点でhttpdからプロセスが終了させられるので(see [http://www.bioinfo.jp/tips.html Tips (CGI, Perl, Unix and etc.)])、exit前に何かの処理を行うことは出来ない。 
    115    * mpm-preforkでは子プロセスが終了するまで、親プロセスはゾンビとして残った。mpm-workerではそのようなことはない模様(こちらも、一度mpm-preforkに切り替えると、workerに戻してもゾンビが残るようになった)。 
    116      * ps aux | grep www-data 
    117 {{{ 
    118 www-data  3767  0.0  0.0      0     0 ?        Z    15:28   0:00 [fork.cgi] <defunct> 
    119 www-data  3768  0.0  0.1  16700   668 ?        S    15:28   0:00 /usr/bin/perl -w /var/www/archive/fork.cgi 
    120 }}} 
    121      * ただし、ゾンビとなった親プロセスは入出力がないため、しばらくするとhttpdによって殺される(see [http://www.bioinfo.jp/tips.html Tips (CGI, Perl, Unix and etc.)])。 
    122  
    123  = making files with random name and random content = 
    124  * [http://d.hatena.ne.jp/perlcodesample/20100413/1270894115 File::Temp - 一時ファイルの作成 / Perlモジュール徹底解説 - サンプルコードによるPerl入門] 
    125  * [http://d.hatena.ne.jp/fbis/20080114/1200307393 ランダムな文字列を生成するString::Random - Unknown::Programming] 
    126  
    127  * makerandom.pl 
    128 {{{ 
    129 #!perl 
    130 #! /usr/bin/perl -w 
    131  
    132 use strict; 
    133 use warnings; 
    134  
    135 use File::Temp qw(tempfile); 
    136 use String::Random; 
    137  
    138 my $filenum = $ARGV[0]; 
    139  
    140 while ($filenum-- > 0) { 
    141     my ($fh, $fname) = tempfile(DIR => '.'); 
    142      
    143     my $length = int(rand(100)); 
    144     print "making $fname ...\n"; 
    145     print $fh String::Random->new->randregex("[A-Za-z0-9]{$length}"); 
    146 } 
    147 }}} 
    148  
    149  = $line = <$hash{'filehandle'}>; # => GLOB = 
    150  * [http://harapeko.asablo.jp/blog/2006/10/13/559447 山カッコ演算子にハッシュの要素を使用することはできない: 国民宿舎はらぺこ 大浴場] 
    151 {{{ 
    152 #!perl 
    153 my %file = (); 
    154  
    155 { 
    156     open my $fh, '<test.txt'    or die; 
    157     $file{'fh'} = $fh; 
    158 } 
    159  
    160 my $line = <$file{'fh'}>;   # NG 
    161 print $line; 
    162 }}} 
    163  * 実行結果 
    164 {{{ 
    165 GLOB(0x161aa34)my %file = (); 
    166  
    167 }}}