- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有这个代码
r = x.run("prog", ["-4"], ['\tab \t cd\n', ' \t ab cd \n', '\ta\b\b\b\tb\n'])
哪里"prog"
是可执行 c 文件的名称,"-4"
是 "prog"
使用的命令行参数和'\tab \t cd\n', ' \t ab cd \n', '\ta\b\b\b\tb\n'
是文件 "prog"
的输入文本
我的run
功能是...
def run(self, prog, args, input):
global debug
result = None
prog = os.path.join(".",prog)
command = [prog] + args
self.createFile(CompileAndExecute.stdinName, input)
cwd = os.getcwd()
os.chdir(self.tmpdir)
stream0 = open(CompileAndExecute.stdinName, "r")
stream1 = open(CompileAndExecute.stdoutName, "w")
stream2 = open(CompileAndExecute.stderrName, "w")
p = None
try:
p = subprocess.call(command,
stdin=stream0, stdout=stream1, stderr=stream2)
except:
result = sys.exc_info()
if p != None:
p.kill()
finally:
stream0.close()
stream1.close()
stream2.close()
os.remove(CompileAndExecute.stdinName)
os.chdir(cwd)
return result
我想在我的 run
中添加另一个参数函数,名为 timeout
。基本上,我想要它,以便如果我的 run
函数运行时间超过 5 秒,我会调用 Sys.exit(1)
并在那里结束。
我的run
的正确调用函数,添加 timeout
参数,将是
r = x.run("prog", ["-4"], ['\tab \t cd\n', ' \t ab cd \n', '\ta\b\b\b\tb\n'], 5)
我的完整代码的总体思路(所有内容都不在这里)是编译并执行一个 C 文件并检查其输出是否是应有的结果。
有人建议我查看 Python3 库文档的第 17.5.1 节,以获取有关如何实现超时的信息,但我无法理解如何实现。我尝试了一些类似问题的解决方案,但没有成功。
有什么帮助吗?
编辑:更多信息请参阅run
功能..
run(self, prog,args=[],input=[])
prog 参数是一个字符串,它指定临时目录中的可执行文件的名称。 args 参数包含一个字符串列表,这些字符串将用作 prog 命名的程序的命令行参数。 run 方法执行程序,提供命令行参数。如果程序在运行时从其标准输入读取,则该标准输入将从名为 input 的参数中获取。输入参数是一个字符串列表;每个字符串代表程序要读取的一行文本输入。当 run 方法返回时,结果要么是 None(显然成功完成),要么是一个字符串(指定程序未执行或未成功完成的原因)。无论函数调用返回什么,都应该检查标准输出流和标准错误输出流。
CompileAndExecute
是 run
的类名发现于..
class CompileAndExecute:
"""The class provides methods for compiling and testing
a program in a temporary directory."""
stdoutName = ".stdout.txt"
stderrName = ".stderr.txt"
stdinName = ".stdin.txt"
# constructor, creates temporary directory
def __init__(self, compiler):
self.compiler = compiler
self.tmpdir = tempfile.mkdtemp()
更新:经过一些帮助后,我收到语法错误
def run(self, prog, args, input):
global debug
result = None
prog = os.path.join(".",prog)
command = [prog] + args
self.createFile(CompileAndExecute.stdinName, input)
cwd = os.getcwd()
os.chdir(self.tmpdir)
stream0 = open(CompileAndExecute.stdinName, "r")
stream1 = open(CompileAndExecute.stdoutName, "w")
stream2 = open(CompileAndExecute.stderrName, "w")
p = None
try:
p = subprocess.call(command,
stdin=stream0, stdout=stream1, stderr=stream2, timeout = 5)
except subprocess.TimeoutExpired:
sys.exit(1)
except:
result = sys.exc_info()
if p != None:
p.kill()
finally:
stream0.close()
stream1.close()
stream2.close()
os.remove(CompileAndExecute.stdinName)
os.chdir(cwd)
return result
对于上述 block 中的代码行,except subprocess.TimeoutExpired:
最佳答案
对于 Python 3+ ,您可以使用 subprocess.call
的 timeout
参数。 ,传入您想要指定的超时,然后如果在子进程终止之前超时到期,子进程将终止子进程并引发 subprocess.TimeoutExpired
异常,您可以捕获该异常,然后调用 sys.exit(1) 。
所以在你的代码中,你会这样做 -
try:
p = subprocess.call(command,
stdin=stream0, stdout=stream1, stderr=stream2, timeout=5)
except subprocess.TimeoutExpired:
import sys #don't need this here, if you have imported anywhere above.
sys.exit(1)
except:
result = sys.exc_info()
if p != None:
p.kill()
此外,subprocess.call
函数返回返回码,而不是进程本身,因此尝试在内执行
不执行任何操作。p.kill()
,除了:
演示 -
import sys
try:
subprocess.call(['python','a.py'],timeout=1)
except subprocess.TimeoutExpired:
sys.exit(1)
其中a.py
是一个Python脚本,循环100000次。
关于python - Python 函数调用超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31665842/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!