저는 파이썬 프로젝트에서 ctypes를 사용합니다. 여기서는 응답을하기 위해 몇 시간이 걸리는 C 함수를 호출해야합니다. 내 문제는 함수 (파이썬 코드에서) 특정 시간 후에 함수를 끝내지 않은 경우에도 함수를 죽이고 싶다는 것입니다.시간 초과가있는 C 함수 호출 - ctypes
멀티 스레드로 시도했지만 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 초 후에 멈추지 않습니다.
알람을 시도했지만 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를 사용하여 아이디어를 얻었습니까?
도움을 주셔서 감사합니다.
마크는
파이썬 코드는 C 코드로 볼 수 없기 때문에 작동하지 않을 수