python - 无法杀死 ipython 进程

标签 python ipython

我在 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/

相关文章:

python - 当提供的参数为 None 时,是否可以强制数据类字段调用其 default_factory?

python - 对 numpy ndarray 进行子类化时,如何正确修改 __getitem__?

python - iPython lprun 不打印输出

python - 如何在下一个 jupyter 单元格中重复使用 plot

python - 如何将 ipython 魔术输出存储到变量中

numpy - numpy中的随机数生成器

python - 使用 Vim 编辑 python 时出错

python - 为什么这个 Canvas 太大了一个像素?

python - 如何将 pandas DataFrame 与内置逻辑连接起来?

google-chrome - ipython 笔记本内核运行,但没有连接