[C++-sig] how do i interrupt a C++ extension?

Amos Anderson nitroamos at gmail.com
Sat Feb 20 00:09:32 CET 2010


>>> ----- Original Message ----
>>> From: Amos Anderson <nitroamos at gmail.com>
>>> To: Development of Python/C++ integration <cplusplus-sig at python.org>
>>> Sent: Wed, February 3, 2010 12:24:13 PM
>>> Subject: [C++-sig] how do i interrupt a C++ extension?
>>>
>>> Hello --
>>>
>>> I've got a python script with C++ extensions. Some of my extensions
>>> take a long time to complete, and I don't want to wait for them to
>>> finish when I'm debugging stuff. However, when I do Ctrl-C in my
>>> terminal, it's completely ignored. So it looks like python is trapping
>>> the signal, but apparently can't do anything with it until the
>>> extension returns control to the python script. I guess ideally,
>>> Ctrl-C would kill the extension and return control to python,
>>> generating an exception, but I'd also be ok if Ctrl-C killed the
>>> python script too.
>>>
>>> I've been googling around, but can't figure out how this seemingly
>>> simple (and desired) task is accomplished. Anybody know how to do it?
>>> Right now, the only solution is Ctrl-Z and kill %1 so I guess that
>>> works for now...
>>>
>>> thanks!
>>>
>>> Amos.


Thanks for all the suggestions, but I just figured out how to do what
I want, and I figured I'd share it with everybody. This was my
coworker's idea. I just wrap my extension in a separate Process, which
can then be killed by the original process as needed. For example:


from multiprocessing import Process
...
      p = Process(target=minimizeStructure, args=(filename,))
      p.start()
      try:
         p.join()
      except:
         p.terminate()
         print "Error: early termination."


and this does exactly what I need.

Amos.


More information about the Cplusplus-sig mailing list