我在 Windows 上使用 iPython。 我目前正在制作一个可以卡在套接字接收上的 python 脚本(我还没有在这部分实现稳健性)。
我正在尝试所有常用的终止进程组合键(至少是我知道的组合键),但 iPython 卡住了,我必须关闭并重新打开它。 我试过Ctrl+C、Ctrl+Z、Ctrl+D,不知道还有没有其他组合。
有人遇到过这种问题吗?
谢谢
亚历山大
最佳答案
一些阻塞操作(与操作系统相关)无法正常中断(见cannot interrupt lock.acquire() whereas I can interrupt time.sleep())
我提出了一些方法,它要求您知道代码中发生了什么。
- 运行一个包含你的代码的线程
- 为
KeyboardInterrupt
运行带有 try/except block 的 main - 异常处理程序应释放/关闭套接字,允许线程完成。
为了简单起见,我创建了一个使用线程锁的示例。您可以将其调整为套接字或任何阻塞资源:
import threading
import time,sys
l = threading.Lock()
def run():
global l
l.acquire()
l.acquire()
t = threading.Thread(target=run)
t.start()
while True:
try:
time.sleep(1)
except KeyboardInterrupt:
print("quitting")
l.release() # now thread can exit safely
break
关于python - 无法杀死 ipython 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45961292/