• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

CTRL + C不会在Python使用CTYPES断对共享库的调用

用户头像
it1352
帮助1

问题说明

当调用在C共享库(动态库)中执行的循环时,Python将不会收到KeyboardInterrupt,并且没有任何响应(或处理)CTRL C。

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.

我该怎么办?

正确答案

#1

除非您使用 PyDLL PYFUNCTYPE ; GIL在ctypes调用期间被释放。因此,如果C代码未安装自己的信号处理程序,Python解释器应通过在主线程中引发 KeyboardInterrupt 来处理SIGINT。

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.

允许Python代码在主线程中运行;您可以将ctypes调用放入后台线程中:

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)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfkhgj
系列文章
更多 icon
同类精品
更多 icon
继续加载