Note that there are some explanatory texts on larger screens.

plurals
  1. POAsynchronous KeyboardInterrupt and multithreading
    primarykey
    data
    text
    <p>It seems that asynchronous signals in multithreaded programs are not correctly handled by Python. But, I thought I would check here to see if anyone can spot a place where I am violating some principle, or misunderstanding some concept.</p> <p>There are similar threads I've found here on SO, but none that seem to be quite the same.</p> <p>The scenario is: I have two threads, reader thread and writer thread (main thread). The writer thread writes to a pipe that the reader thread polls. The two threads are coordinated using a <code>threading.Event()</code> primitive (which I assume is implemented using <code>pthread_cond_wait</code>). The main thread waits on the <code>Event</code> while the reader thread eventually sets it.</p> <p>But, if I want to interrupt my program while the main thread is waiting on the <code>Event</code>, the KeyboardInterrupt is not handled asynchronously.</p> <p>Here is a small program to illustrate my point:</p> <pre><code>#!/usr/bin/python import os import sys import select import time import threading pfd_r = -1 pfd_w = -1 reader_ready = threading.Event() class Reader(threading.Thread): """Read data from pipe and echo to stdout.""" def run(self): global pfd_r while True: if select.select([pfd_r], [], [], 1)[0] == [pfd_r]: output = os.read(pfd_r, 1000) sys.stdout.write("R&gt; '%s'\n" % output) sys.stdout.flush() # Suppose there is some long-running processing happening: time.sleep(10) reader_ready.set() # Set up pipe. (pfd_r, pfd_w) = os.pipe() rt = Reader() rt.daemon = True rt.start() while True: reader_ready.clear() user_input = raw_input("&gt; ").strip() written = os.write(pfd_w, user_input) assert written == len(user_input) # Wait for reply -- Try to ^C here and it won't work immediately. reader_ready.wait() </code></pre> <p>Start the program with './bug.py' and enter some input at the prompt. Once you see the reader reply with the prefix 'R>', try to interrupt using <code>^C</code>.</p> <p>What I see (Ubuntu Linux 10.10, Python 2.6.6) is that the <code>^C</code> is not handled until after the blocking <code>reader_ready.wait()</code> returns. What I expected to see is that the <code>^C</code> is raised asynchronously, resulting in the program terminating (because I do not catch KeyboardInterrupt).</p> <p>This may seem like a contrived example, but I'm running into this in a real-world program where the <code>time.sleep(10)</code> is replaced by actual computation.</p> <p>Am I doing something obviously wrong, like misunderstanding what the expected result would be?</p> <p>Edit: I've also just tested with Python 3.1.1 and the same problem exists.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload