3

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

A.Joly
  • 2,317
  • 2
  • 20
  • 25

2 Answers2

2

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
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
-1

Try the following command:

exit

or try to restart the kernel.

Samir Alhejaj
  • 157
  • 2
  • 16