Unfortunately, this didn’t help. However, I eventually created a separate implementation for Python functions:
pyexec("""
import threading
import time
import ctypes
def _async_raise(tid, exctype):
'''Abort the thread'''
if not isinstance(tid, int):
tid = tid.ident
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
if res == 0:
raise ValueError("Invalid thread id")
elif res > 1:
# If it returns a number greater than one
# We're in trouble, and we try to revert the effect
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), None)
raise SystemError("PyThreadState_SetAsyncExc failed")""", Main)
@pyexec ```
def py_measure_time(func, maxtime, *args):
def wrapper():
func(*args)
thread = threading.Thread(target=wrapper)
thread.start()
start_time = time.time()
thread.join(timeout=maxtime)
if thread.is_alive():
_async_raise(thread, SystemExit)
return float("Inf")
else:
return time.time() - start_time``` => py_measure_time
time_function(f::Py, args...; maxtime=1) =
pyconvert(Number, py_measure_time(f, maxtime, args...))