我经常使用以下模式为Perl中特定代码片段的运行时间设置上限:
my $TIMEOUT_IN_SECONDS = 5;
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm($TIMEOUT_IN_SECONDS);
# do stuff that might timeout.
alarm(0);
};
if ($@) {
# handle timeout condition.
}
我的问题:
这样做对吗?
是否有任何情况下运行时间可以超过$TIMEOUT_IN_SECOND
例如,如果一个函数的运行时间超过3秒,我就会尝试让它超时。我在使用信号和警报,但警报从来不会响。我想要一个对任何函数都有效的超时机制。作为我面临的问题的一个例子:
import signal
def foobar():
x = 42
while x >= 20:
if x >= 40:
x = 23
return x
def handle_alarm(*args):
print("Alarm raised")
raise TimeoutException("timeout r
因此,我对这个问题进行了深入的研究(至少我是这么认为的)。我试图将get()的alarm超时设置为60秒,但它没有被捕获并将运行超过60秒,而且每当www::机械化::timed构造函数达到默认超时(180秒)时,我都会收到以下错误:
在/usr/lib/perl5/site_perl/5.10.0/WWW/Mechanize/Timed.pm第52行的加法(+)中使用未初始化值。
代码:
use WWW::Mechanize::Timed;
use HTTP::Cookies;
use Try::Tiny;
my $ua = WWW::Mechanize::Timed->new(
a
首先,我是C公司的新手,我继承了一些遗留的C代码。在这个应用程序中,超时不能正常工作。它使用信号和警报来创建超时。我读过这两篇文章,但仍有问题。
signal(SIGALRM, timeouthandler);
alarm(3);
...(connection to a server that may hang)...
alarm(0);
timeouthandler(){
...(connection to a server that may hang)...
}
现在,这很好,直到我必须将超时添加到代码的不同部分。
signal(SIGALRM, timeouthandler2);
我有一个调用其他几个函数的函数,例如
sub do_work {
send_mail();
send_soap_envelope();
send_rpc();
}
被调用的函数可能会挂起,所以我想在超时后停止它们。我希望避免派生,因为在我的上下文中它是昂贵的(例如,每次派生之后都需要重新创建数据库句柄)。我想出了以下方案:
sub timeout {
my ($code) = @_;
eval {
alarm 2;
local $SIG{ALRM} = sub { die 'timeout' };
这是我的节目
my $input;
my $finish = 0;
my $timer = 5; # 5 seconds
eval {
while( ! $finish ) {
local $SIG{ALRM} = sub {
# check counter and set alarm again
if (--$timer) { alarm 1 }
# no more waiting
else { die "timeout getting the input \n" }
我正在尝试在一个使用标准的读取-求值-打印循环的程序中实现警报。
代码的示例如下所示:
while True:
input = get_input() # A function that waits for input and
# returns the input once it is obtained
set_alarm(interpret(input)) # A function that sets the alarm
Set_alarm使用threading.Timer类。不过,这有一个问题。计时器按时间激活,但当get
我在和合作。我处理我生成的发送/接收信号的流程图。这些流程图初始化并启动,但它们不会将控制流返回给我的应用程序:
我导入了time
while time.time() < endtime:
# invoke GRC flowgraph for 1st sequence
if not seq1_sent:
tb = send_seq_2.top_block()
tb.Run(True)
seq1_sent = True
if time.time() < en
我正在尝试对python MySQLDB中的查询执行一个时间限制。在这种情况下,我无法控制查询,但需要确保查询不超过设定的时间限制。我尝试过使用signal.SIGALRM中断要执行的调用,但这似乎不起作用。该信号被发送,但直到调用完成后才会被捕获。
我编写了一个测试用例来证明这种行为:
#!/usr/local/bin/python2.6
import time
import signal
from somewhere import get_dbc
class Timeout(Exception):
""" Time Exceded ""
我看到一个代码说明如何进行安全的信号处理。我不明白为什么信号处理程序会再次调用signal (sig, catch_alarm);。为什么要这么做?没有它,代码也能工作。
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* This flag controls termination of the main loop. */
volatile sig_atomic_t keep_going = 1;
/* The signal h
我正在编写类似于下面的内容(),我需要实现一个计时器来监视会话的待定状态,并在它们超过某个阈值时终止它们,这样做最好的方法是什么?
请注意,我正在寻找算法。
Credit-Control Application Related Parameters
Tx timer
When real-time credit-control is required, the credit-control
client contacts the credit-control server before and while the
service is provided to an end user. Due
我从GNU库中获取了这个例子。我想知道为什么他们调用signal()函数两次,第一次在设置信号处理程序时在main()中,第二次在处理程序函数本身中。
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
/* This flag controls termination of the main loop. */
volatile sig_atomic_t keep_going = 1;
/* The signal handler just clears the flag and re-en
我很绝望。
我的代码在视频中读取nframe,有时代码会在没有原因的情况下停止,也不会出错。所以我决定以某种方式引起一个错误。问题是,代码确实会引发一个错误,但是由于某种原因它会忽略它,并且只是正常工作。
*我提供了一个完全相同方法工作的代码块。
处理程序:
def handler(signum,frame):
print("error") ## This is printed
raise Exception('time out') ## I guess this is getting raised
代码部分我想包装:
for i in range(0,
您好,我在我的python代码中嵌入了一个时间约束,它正在运行一个带有函数的fortran代码。然而,我意识到,对另一个函数施加时间限制的函数并不会终止代码,而只是将其留在后台并跳过它。我想要做的是,当代码超过约束时间时,终止代码。这是我用来限制时间的代码,取自。
def timeout(func, args=(), kwargs={}, timeout_duration=15, default=1):
import signal
class TimeoutError(Exception):
pass
def handler(signum, fram
我需要创建一个接受两个参数WAIT, and TIME的c文件。
代码应该是first叉()一个子进程,它可以工作W秒,然后退出。父进程应等待子进程,但最长时间为T秒。
在等待的T秒之后,父进程应该停止等待,并打印消息“T秒之后超时”,然后退出。
另一方面,如果子进程在超时之前终止,则父程序应该打印消息“子完成”。然后离开。
我想尝试使用alarm()函数来完成这个任务,但是我仍然无法理解到底是如何做到的。
#include <stdio.h>
int int main(int argc, char const *argv[])
{
int W = atoi(argv[1
每当使用WebRequest测试internet连接时,都可以正常工作。然而,当我试图连接到互联网(关闭wifi之后)并拥有wifi的状态到"no access to internet"时,如果我再次执行网络请求,它就是不起作用。我已经调试过了,发现这个问题就在网上:
objResp = objWebReq.GetResponse
它只需要很长的时间,却没有完成。
下面是测试连接的函数:
Dim objUrl As New System.Uri("http://www.google.com")
Dim objWebReq As System.Net.WebReq