keyboardinterrupt

Equivalent of thread.interrupt_main() in Python 3

前提是你 提交于 2019-12-12 21:34:28
问题 In Python 2 there is a function thread.interrupt_main() , which raises a KeyboardInterrupt exception in the main thread when called from a subthread. This is also available through _thread.interrupt_main() in Python 3, but it's a low-level "support module", mostly for use within other standard modules. What is the modern way of doing this in Python 3, presumably through the threading module, if there is one? 回答1: Well raising an exception manually is kinda low-level, so if you think you have

python 2.7: how to catch keyboard interrupt in program with more than 25 threads

纵饮孤独 提交于 2019-12-12 04:05:41
问题 I want to stop my program when the user presses ctrl-C. The following answer suggests catching the KeyboardInterrupt exception. python: how to terminate a thread when main program ends Sometimes it works. But in the following example, it stops working after I increase the number of threads from 25 to 30. import threading, sys, signal, os stderr_lock = threading.Lock() def Log(module, msg): with stderr_lock: sys.stderr.write("%s: %s\n" % (module, msg)) class My_Thread(threading.Thread): def _

Assembly 8086 listening keyboard interrupt

邮差的信 提交于 2019-12-11 04:36:09
问题 I have exactly the same question as this: Listen to keyboard while drawing But the first answer (accepted one) listens keyboard for exactly once. So how can I modify my code so that I can listen keyboard interrupt for more then once. This is my code: .model small draw_row Macro x Local l1 ; draws a line in row x from col 10 to col 300 MOV AH, 0CH MOV AL, 4 MOV CX, 0 MOV DX, x L1: INT 10h INC CX CMP CX, 320 JL L1 EndM .stack 100h .data height1 dw 51 width1 dw 65 v1 dw ? v2 dw ? CUR_POSX_USER

KeyboardInterrupt unpredictable in Python 2.7 under ipython, how can I make it *always* abort current evaluation?

不问归期 提交于 2019-12-11 02:21:24
问题 I'm writing python code to do numerical analysis, and I've been using ipython or ipython -pylab as my command line interface. I often run into situations where some code is taking for-freaking-ever to run, and I need to stop it. However, Ctrl-C is problematic; sometimes it works, sometimes it doesn't do anything, and sometimes it quits the whole process ( very annoying.) How can I make it so that hitting Ctrl-C always always works? It seems as if the times that it doesn't work are those where

python gevent: unexpected output in KeyboardInterrupt

一个人想着一个人 提交于 2019-12-10 17:33:37
问题 Running this code import gevent def f(): while True: gevent.sleep(1) if __name__ == '__main__': tasks = (gevent.spawn(f),) try: gevent.wait(tasks) except KeyboardInterrupt: print("KeyboardInterrupt trapped") and then pressing a Ctrl-C, give me this output: $ python receiver.py ^CKeyboardInterrupt Tue Aug 8 00:56:04 2017 KeyboardInterrupt trapped Why? It seems someone is writing the exit time on output. How can I prevent that KeyboardInterrupt in the first line and the date in the second? 回答1:

Python KeyboardInterrupt in Multithreading

橙三吉。 提交于 2019-12-08 05:30:32
Can anyone please tell me why the KeyboardInterrupt is not working here? I need to end both of the threads by the press of CTRL+C. The two threads - Timer thread and Web server thread. Here is the code - import threading import thread import time import urllib2 import httplib from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler from SocketServer import ThreadingMixIn import Queue import signal import sys q = Queue.Queue() j = Queue.Queue() h = "threading working!" class SignalHandler: stopper = None def __init__(self,stopper): self.stopper = stopper def __call__(self, signum, frame):

Python KeyboardInterrupt in Multithreading

爱⌒轻易说出口 提交于 2019-12-08 03:22:29
问题 Can anyone please tell me why the KeyboardInterrupt is not working here? I need to end both of the threads by the press of CTRL+C. The two threads - Timer thread and Web server thread. Here is the code - import threading import thread import time import urllib2 import httplib from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler from SocketServer import ThreadingMixIn import Queue import signal import sys q = Queue.Queue() j = Queue.Queue() h = "threading working!" class SignalHandler

Stop a running command in PyDev Console

我的未来我决定 提交于 2019-12-05 18:55:22
I use the PyDev console to run long scripts, and often I wish to stop in the middle of a command. In a regular python shell I press ctrl+c and it stops the command with keyboard interrupt. But in PyDev consoles it does a text copy instead. How do I stop a command in this console without terminating it? If you are running the scripts from an interactive console with newish PyDev (not sure which version this feature arrived) then you can press the Yellow stop to achieve what you want. If you are simply running the script (not via the interactive console) then I recommend a feature request at

When is KeyboardInterrupt raised in Python?

不羁的心 提交于 2019-12-05 03:28:38
All the docs tell us is, Raised when the user hits the interrupt key (normally Control-C or Delete ). During execution, a check for interrupts is made regularly. But from the point of the code, when can I see this exception? Does it occur during statement execution? Only between statements? Can it happen in the middle of an expression? For example: file_ = open('foo') # <-- can a KeyboardInterrupt be raised here, after the successful # completion of open but prior to the try? --> try: # try some things with file_ finally: # cleanup Will this code leak during a well-timed KeyboardInterrupt ? Or

Python threads with os.system() calls. Main thread doesn't exit on ctrl+c

久未见 提交于 2019-12-04 10:12:09
Please don't consider it a duplicate before reading, There are a lot of questions about multithreading and keyboard interrupt , but i didn't find any considering os.system and it looks like it's important. I have a python script which makes some external calls in worker threads. I want it to exit if I press ctrl+c But it look like the main thread ignores it. Something like this: from threading import Thread import sys import os def run(i): while True: os.system("sleep 10") print i def main(): threads=[] try: for i in range(0, 3): threads.append(Thread(target=run, args=(i,))) threads[i].daemon