Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my threading/multiprocessing python script not exiting properly?
    primarykey
    data
    text
    <p>I have a server script that I need to be able to shutdown cleanly. While testing the usual <code>try..except</code> statements I realized that <code>Ctrl-C</code> didn't work the usual way. Normally I'd wrap long running tasks like this</p> <pre><code>try: ... except KeyboardInterrupt: #close the script cleanly here </code></pre> <p>so the task could be shutdown cleanly on <code>Ctrl-C</code>. I have never ran into any problems with this before, but somehow when I hit <code>Ctrl-C</code> when this particular script is running the script just exits without catching the <code>Ctrl-C</code>.</p> <p>The initial version was implemented using <code>Process</code> from <code>multiprocessing</code>. I rewrote the script using <code>Thread</code> from <code>threading</code>, but same issue there. I have used <code>threading</code> many times before, but I am new to the <code>multiprocessing</code> library. Either way, I have never experienced this <code>Ctrl-C</code> behavior before.</p> <p>Normally I have always implemented sentinels etc to close down <code>Queues</code> and <code>Thread</code> instances in an orderly fashion, but this script just exits without any response.</p> <p>Last, I tried overriding <code>signal.SIGINT</code> as well like this</p> <pre><code>def handler(signal, frame): print 'Ctrl+C' signal.signal(signal.SIGINT, handler) ... </code></pre> <p>Here <code>Ctrl+C</code> was actually caught, but the handler doesn't execute, it never prints anything.</p> <p>Besides the <code>threading</code> / <code>multiprocessing</code> aspect, parts of the script contains <code>C++</code> <code>SWIG</code> objects. I don't know if that has anything to do with it. I am running Python 2.7.2 on OS X Lion.</p> <p>So, a few questions:</p> <ol> <li>What's going on here?</li> <li>How can I debug this?</li> <li>What do I need to learn in order to understand the root cause?</li> </ol> <p>PLEASE NOTE: The internals of the script is proprietary so I can't give code examples. I am however very willing to receive pointers so I could debug this myself. I am experienced enough to be able to figure it out if someone could point me in the right direction.</p> <p><strong>EDIT:</strong> I started commenting out imports etc to see what caused the weird behavior, and I narrowed it down to an import of a <code>C++ SWIG</code> library. Any ideas why importing a <code>C++ SWIG</code> library 'steals' <code>Ctrl-C</code>? I am not the author of the guilty library however and my SWIG experience is limited so don't really know where to start...</p> <p><strong>EDIT 2:</strong> I just tried the same script on a windows machine, and in Windows 7 the <code>Ctrl-C</code> is caught as expected. I'm not really going to bother with the OS X part, the script will be run in an Windows environment anyway.</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.
 

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