A.Joly
A.Joly

Reputation: 2387

Impossible to kill ipython process

I use iPython on Windows. I'm currently making a python script that can be stuck on socket reception (I have not implemented robustness on this part yet).

I'm trying every usual killing process key combinations (at least the ones I know) but iPython is stuck and I have to close and reopen it. I tried Ctrl+C, Ctrl+Z, Ctrl+D, I don't know if there are other combinations.

Has anyone gone through this kind of problem ?

thank you

Alexandre

Upvotes: 3

Views: 992

Answers (2)

Jean-François Fabre
Jean-François Fabre

Reputation: 140168

Some blocking operations (related to the operating system) cannot be interrupted properly (see cannot interrupt lock.acquire() whereas I can interrupt time.sleep())

I propose some approach which requires you to know what's going on in your code.

  • run a thread with your code in it
  • run a main with a try/except block for KeyboardInterrupt
  • the handler of the exception shall release/close the socket, allowing the thread to finish.

I've created an example using thread locks for simplicity's sake. You can adapt this to sockets or whatever blocking resource:

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

Upvotes: 2

Samir Alhejaj
Samir Alhejaj

Reputation: 157

Try the following command:

exit

or try to restart the kernel.

Upvotes: -1

Related Questions