Perl Thread子进程从主线程中窃取STDOUT

时间:2013-07-17 00:07:37

标签: multithreading perl stdout

我有一个子进程正在窃取FastCGI中的STDOUT连接(通过Nginx)。客户端通过http将文件上传到我的网络服务器,然后网络服务器通过bash命令解密数据流,该命令位于子进程的“open”命令中。问题是“嗨,我在里面”对客户来说,但不是“嗨,我在外面”。为什么子进程的STDOUT最终通过FastCGI发送到客户端而不是“嗨,我在外面”。实际上,在加入子进程后在主进程中完成的任何打印都不会在客户端浏览器中显示。

至于我的脚本的运行情况,基本上,它会分叉子进程并将上传文件流转发给子进程。然后子进程解密文件流并将清除数据发送到unix套接字。回到主进程,打开文件句柄以侦听unix套接字。当写入来自子进程时,主进程通过unix socket(socat)接收明文。

这是我的代码:

$uploadFH = $query->upload('uplink'); # <-- a client is uploading a file
open(my $clearFH, " socat - UNIX-LISTEN:$workdir/$randompath.decrypt | ") || die "can't decrypt";
my $Q = new Thread::Queue;
my $thread = threads->create(sub{
    my $q = shift;
    my $key = $q->dequeue;
    my $upfileno = $q->dequeue;
    open FH, "<&=$upfileno" or die $!;
    # open data stream, and decrypt it using openssl on the command line
    open(my $sfh, "| openssl aes-256-cbc -pass pass:$key -salt -d |  socat - GOPEN:$workdir/$randompath.sessiondecrypt,append ") || die "can't open here";
    while(my $line = <FH>){
        print $sfh $line;
    }
    close $sfh;
    print "Hi, I am inside.";
}, $Q);
$Q->enqueue( $SymmetricKey );
$Q->enqueue( fileno $uploadFH );
# start scooping up decrypted data from Unix socket
while(<$clearFH){ ...}
$thread->join(); # or $thread->detach(), same result
print "Hi, I am outside.";
# ...rest of program continues below

作为进一步的提示,我发现上面的子进程是“main :: STDOUT”的默认打印句柄。因此,看起来子进程“窃取”主进程的STDOUT输出,并且当子进程完成时,主进程也是如此。

0 个答案:

没有答案