【问题标题】:How can I implement a timeout for a qx(command)?如何为 qx(command) 实现超时?
【发布时间】:2010-03-23 08:03:57
【问题描述】:

我如何在这段代码中实现超时:如果“hwinfo --usb”命令在一定时间后没有返回任何内容,(停止命令并)返回或死于子_usb_device。

#!/usr/bin/env perl
use warnings; 
use strict;


sub _usb_device {
    my @array;
    {
    local $/ = "";
    @array = qx( hwinfo --usb );
    }
    ...
    ...
}

【问题讨论】:

    标签: perl timeout qx


    【解决方案1】:

    超时通常使用alarms 完成。

    
     sub _usb_device 
     {
        # Scope array
        my @array;
    
        # Try shell command
        eval
        {
            local $SIG{ALRM} = sub { die "timeout\n" };
            local $/ = "";
            alarm 10;
            @array = qx( hwinfo --usb );
            alarm 0;
        };
    
        # Catch and rethrow non timout errors
        die $@ if $@ && $@ ne "timeout\n";
    
        # Done
        return @array;
     }
    

    【讨论】:

      猜你喜欢
      • 2013-03-09
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多