Why does the asyncio’s event loop suppress the KeyboardInterrupt on Windows?

There is workaround for Windows. Run another corouting which wake up loop every second and allow loop to react on keyboard interrupt Example with Echo server from asyncio doc async def wakeup(): while True: await asyncio.sleep(1) loop = asyncio.get_event_loop() coro = loop.create_server(EchoServerClientProtocol, ‘127.0.0.1’, 8888) server = loop.run_until_complete(coro) # add wakeup HACK loop.create_task(wakeup()) try: loop.run_forever() except … Read more

Why can’t I handle a KeyboardInterrupt in python?

Asynchronous exception handling is unfortunately not reliable (exceptions raised by signal handlers, outside contexts via C API, etc). You can increase your chances of handling the async exception properly if there is some coordination in the code about what piece of code is responsible for catching them (highest possible in the call stack seems appropriate … Read more

Python: How to prevent subprocesses from receiving CTRL-C / Control-C / SIGINT

Somebody in the #python IRC-Channel (Freenode) helped me by pointing out the preexec_fn parameter of subprocess.Popen(…): If preexec_fn is set to a callable object, this object will be called in the child process just before the child is executed. (Unix only) Thus, the following code solves the problem (UNIX only): import subprocess import signal def … Read more

threading ignores KeyboardInterrupt exception

Try try: thread=reqthread() thread.daemon=True thread.start() while True: time.sleep(100) except (KeyboardInterrupt, SystemExit): print ‘\n! Received keyboard interrupt, quitting threads.\n’ Without the call to time.sleep, the main process is jumping out of the try…except block too early, so the KeyboardInterrupt is not caught. My first thought was to use thread.join, but that seems to block the main … Read more

Capture keyboardinterrupt in Python without try-except

Yes, you can install an interrupt handler using the module signal, and wait forever using a threading.Event: import signal import sys import time import threading def signal_handler(signal, frame): print(‘You pressed Ctrl+C!’) sys.exit(0) signal.signal(signal.SIGINT, signal_handler) print(‘Press Ctrl+C’) forever = threading.Event() forever.wait()

Catching KeyboardInterrupt in Python during program shutdown

Checkout this thread, it has some useful information about exiting and tracebacks. If you are more interested in just killing the program, try something like this (this will take the legs out from under the cleanup code as well): if __name__ == ‘__main__’: try: main() except KeyboardInterrupt: print(‘Interrupted’) try: sys.exit(0) except SystemExit: os._exit(0)

Keyboard Interrupts with python’s multiprocessing Pool

This is a Python bug. When waiting for a condition in threading.Condition.wait(), KeyboardInterrupt is never sent. Repro: import threading cond = threading.Condition(threading.Lock()) cond.acquire() cond.wait(None) print “done” The KeyboardInterrupt exception won’t be delivered until wait() returns, and it never returns, so the interrupt never happens. KeyboardInterrupt should almost certainly interrupt a condition wait. Note that this … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)