我使用Term :: Readline创建shell提示符,如下所示 -
my $expire_time = time + 25; # 25 sec timeout for testing and shared with thread
my $prompt = 'SHELL> ';
my $terminal = Term::ReadLine->new('Shell Interface');
my $attribs = $terminal->Attribs;
$terminal->ornaments(0); # disable ornaments.
while ( defined( $_ = $terminal->readline($prompt) ) ) {
if (/^\s*quit\s*$/xms) {
# Cleanup the global parameters
#...some cleanup code
# Signal the thread to terminate, and then detach
# it so that it will get cleaned up automatically
# I will be using thread to control this customize shell
$thread->kill('KILL')->detach();
last;
}
}
$thread = threads->create(
sub {
local $SIG{KILL} = sub { threads->exit() };
eval {
my $buffer_interval = int( sqrt( $expire_time - time() ) );
my $term = Term::ReadLine->new('Handle session of Shell');
$term->ornaments(0);
my $message = "Do you want to extend your session?[y/n]- ";
INNER: while ( $expire_time > time() ) {
my $diff = $expire_time - time();
sleep 1;
print "diff - $diff, buffer_interval - $buffer_interval\n";
if ( $diff == $buffer_interval ) {
say "Your session will expire in $diff seconds due to inactivity.";
while ( defined( $_ = $term->readline($message) ) )
{
if (/^y$/i) {
$expire_time += $timeout;
say "refreshed expire_time - $expire_time";
}
next INNER;
}
}
}
&killproc('SHELL')
or die "cannot kill SHELL, aborting";
};
}
);
此线程将处理SHELL的超时,$ expire_time设置为某个值,例如90秒,超时的平方根意味着9秒,然后用户将收到警告消息,即您的会话将很快到期..如果用户将输入'y'会话将被$ expire_time扩展。
但是当在线程中创建Term :: ReadLine对象时,它的行为方式不正确,有时在进入'y'时会话延长,有时不会。
示例如下 -
到期时间是 - 1430279631
SHELL> diff - 25,buffer_interval - 5
diff - 24,buffer_interval - 5
diff - 23,buffer_interval - 5
diff - 22,buffer_interval - 5
diff - 21,buffer_interval - 5
diff - 20,buffer_interval - 5
diff - 19,buffer_interval - 5
diff - 18,buffer_interval - 5
diff - 17,buffer_interval - 5
diff - 16,buffer_interval - 5
diff - 15,buffer_interval - 5
diff - 14,buffer_interval - 5
diff - 13,buffer_interval - 5
diff - 12,buffer_interval - 5
diff - 11,buffer_interval - 5
diff - 10,buffer_interval - 5
diff - 9,buffer_interval - 5
diff - 8,buffer_interval - 5
diff - 7,buffer_interval - 5
diff - 6,buffer_interval - 5
diff - 5,buffer_interval - 5
由于不活动,您的会话将在5秒后到期
你想延长会话时间吗?[y / n] - y
SHELL>
在()循环中没有打印消息,说明刷新到期时间......
这是问题吗,因为我已经在某个Term :: Readline提示符中并且再次在其中使用Term :: Readline提示符?
有任何潮红问题吗?