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

Gustavo Carneiro gjcarneiro at gmail.com
Thu Feb 4 00:57:29 CET 2010


On Wed, Feb 3, 2010 at 9:42 PM, Ralf W. Grosse-Kunstleve <rwgk at yahoo.com>wrote:

> It would be a nice feature to have, but in 8+ years working with
> Boost.Python I never truly needed it.
>
> I figure if your extension runs a long time it must have some layers of
> loops. If you can modify the
> extension code, I'd reorganize it to move the outer loop into Python. If
> the extension is a function,
> I'd turn it into a class like this:
>
>  calc = calculation()
>  while (calc.is_not_finished()):
>    calc.inner_loop()
>
> If you figure out how to make Ctrl-C work portably (!), post it here!
>

I wouldn't advise this solution exactly like this.  It may work, but is
going to be slow.  Moving loops into Python makes the computation probably a
lot slower.

The solution I adopted for the NS-3 simulator (ns3.Simulator.Run()) was kind
of hybrid.  There is an outer loop, and there is an API to run a single
iteration of that loop.  The ns3.Simulator.Run() wrapper[1] runs iterations
of the loop the following way: we run iterations while there are events to
process; every 100 iterations we 1) acquire the GIL, 2) call
PyErr_CheckSignals() and check the result of PyErr_Occurred(), 3) release
the GIL again.

[1] See _wrap_Simulator_Run in
http://code.nsnam.org/ns-3-dev/file/0ca25e25b116/bindings/python/ns3module_helpers.cc


> Ralf
>
>
> ----- 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.
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Gustavo J. A. M. Carneiro
INESC Porto, Telecommunications and Multimedia Unit
"The universe is always one step beyond logic." -- Frank Herbert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20100203/6215cc67/attachment.htm>


More information about the Cplusplus-sig mailing list