CTRL+C doesn't interrupt call to shared-library using CTYPES in Python

雨燕双飞 提交于 2019-12-13 13:02:33

问题


When calling a loop being performed in a C shared-library (dynamic library), Python will not receive a KeyboardInterrupt, and nothing will respond (or handle) CTRL+C.

What do I do?


回答1:


Unless you use PyDLL or PYFUNCTYPE; the GIL is released during the ctypes calls. Therefore the Python interpreter should handle SIGINT by raising KeyboardInterrupt in the main thread if the C code doesn't install its own signal handler.

To allow the Python code to run in the main thread; you could put the ctypes call into a background thread:

import threading

t = threading.Thread(target=ctypes_call, args=[arg1, arg2, ...])
t.daemon = True
t.start()
while t.is_alive(): # wait for the thread to exit
    t.join(.1)



回答2:


You will have to declare a signal handler for SIGINT, within the C, which is, hopefully, your project.




回答3:


I used a threaded solution but then switched to a signal one. The work-around I use is to send SIGTERM from SIGINT handler, e.g.:

signal.signal(signal.SIGINT, lambda s, f : os.kill(os.getpid(), signal.SIGTERM))

Here I just want to save a core idea of the solution to find it faster next time and the reason why I have changed the approach. The threaded variant does not suite for me because OpenMP becomes significantly slower when it is called not from the main thread.



来源:https://stackoverflow.com/questions/14271697/ctrlc-doesnt-interrupt-call-to-shared-library-using-ctypes-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!