- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在一个 python 项目中使用 ctypes,我需要调用一些 C 函数,这可能需要几个小时才能给出响应。我的问题是我想在一段时间后终止函数(从 python 代码),即使函数还没有完成计算。
我尝试使用多线程,但它不会停止 C 函数。这是我的代码。
lib_iw = cdll.LoadLibrary("./iw.so")
iw_solve = lib_iw.solve_iw # the C function which can take some hours for responding
iw_solve.restype = c_void_p
iw_solve.argtypes = [c_void_p]
def iw_solve2() :
iw_solve(None)
def iw_solve_wtimeout(time_out) : # the function which thow $iw_solve and kill the execution of $iw_solver after $time_out seconds (but it doesn't work)
t_solve = threading.Thread(None,iw_solve2,None,(),None)
t_solve.run()
t = time.time()
while(t_solve.is_alive() and ((time.time() - t) < 2)) :
time.wait(0.3)
if(t_solve.is_alive()) :
t_solve._Thread__stop()
print "iw_solve was killed"
else :
print "iw_solve has respond"
它不起作用:当我调用 iw_solve_wtimeout(10) 时;该功能不会在 10 秒后停止。
我试过 alarm 但它不会停止 c 函数。这是我的代码。
lib_iw = cdll.LoadLibrary("./iw.so")
iw_solve = lib_iw.solve_iw # the C function which can take some hours for responding
iw_solve.restype = c_void_p
iw_solve.argtypes = [c_void_p]
def iw_solve_withtimeout(time_out) : # the function which thow $iw_solve and kill the execution of $iw_solver after $time_out seconds (but it doesn't work)
signal.signal(signal.SIGALRM, handler)
signal.alarm(time_out)
try :
print "debut iw_solve"
signal.alarm(time_out)
iw_solve(None)
except Exception, exc:
print exc
return None;
此代码也不起作用:当我调用 iw_solve_wtimeout(10) 时;该功能不会在 10 秒后停止。
对于使用 ctypes 做这件事,您有什么想法吗?
非常感谢您的帮助。
马克
最佳答案
这行不通,因为 Python 代码无法查看 C 代码以阻止它运行。这种方法只能用于纯 Python 代码。
即使可行,停止线程的方法也是错误的。您应该将线程视为一种合作努力。如果你想让一个线程停止,那么你需要让它停止,然后等待直到它停止。使用合作而不是武力可以避免各种可怕的问题。
您需要做的是修改您的 C 代码并允许它发出取消信号。 C 代码需要定期检查此信号。您可以使用 ctypes 回调或许多其他方式来实现它。
但从根本上说,您需要在 C 代码中提供明确的取消机制。
关于python - 超时调用 C 函数 - ctypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5247353/
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
我是一名优秀的程序员,十分优秀!