[C++-sig] Why can't kill process by Ctrl-c in a boost.python module with an endless loop

Niall Douglas s_sourceforge at nedprod.com
Tue Dec 25 00:10:06 CET 2012


In your C++ you simply need to ensure that Python gets a regular 
chance to execute in any native loop you execute so it can do its 
housekeeping. One way is PyErr_CheckSignals(), but that won't do for 
long running loops, only short ones, because it doesn't do much 
housekeeping.

In my own code I simply called a small function every 1024 loop 
cycles which instantly returned e.g.

if(!(idx++ & 1023))
  call_my_null_python_function();

def my_null_python_function:
    pass

This works very well indeed.

Niall


On 23 Dec 2012 at 17:02, simon zhang wrote:

> This is the answer from the stackoverflow.
> 
>    while (true) { //endless loop
>        ++it;
>        std::cout<< it <<std::endl;
>        sleep(3);
>        if(PyErr_CheckSignals() == -1) {
>            exit(1);
>        }
>    }
> 
> 
> 2012/12/23 Jaedyn K. Draper <jaedyn.cppsig at jaedyn.co>
> 
> >  Oh, my mistake. Not sure then, I've only embedded, never extended. Maybe
> > someone else can help. :(
> >
> >
> > On 12/23/2012 1:59 AM, simon zhang wrote:
> >
> > But I  don't call Py_Initialize().I call C++ code in Python.Don't  embed
> > the Python to C++...
> >
> > 2012/12/23 Jaedyn K. Draper <jaedyn.cppsig at jaedyn.co>
> >
> >>  Instead of Py_Initialize() (wherever it is you call it), try calling
> >> Py_InitializeEx(0). Py_Initialize() (or Py_InitializeEx(1)) binds signal
> >> handlers (including SIGINT) to send python exceptions instead of killing
> >> the process. This may be what's hitting you.
> >>
> >>
> >> On 12/23/2012 1:44 AM, simon zhang wrote:
> >>
> >>  I have make a boost.python module with an endless loop.But I can't kill
> >> the process by ctrl-c.The following is an example.
> >>
> >>   C++
> >>
> >> #include <boost/python.hpp>#include <boost/python.
> >> module.hpp>#include <boost/python.
> >> def.hpp>#include <iostream>
> >> usring namespace boost::python;
> >> void foo() {
> >>    int it=0;
> >>    while (true) {                     //endless loop
> >>        ++it;
> >>        std::cout<< it <<std::endl;
> >>        sleep(3);
> >>    }}
> >>
> >> BOOST_PYTHON_MODULE(ctopy){
> >>     def("foo",foo);}
> >>
> >>  python:
> >>
> >> import ctopy
> >> ctopy.foo()
> >>
> >>  result:
> >>
> >> 1234.....................
> >>
> >>  I can't kill the foreground process by Ctrl-c.why the module don't
> >> accept signal "SIGINT" that was sent by Ctrl-c.How to make it work.
> >>
> >>
> >>  _______________________________________________
> >> Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig
> >>
> >>
> >>
> >> _______________________________________________
> >> Cplusplus-sig mailing list
> >> Cplusplus-sig at python.org
> >> http://mail.python.org/mailman/listinfo/cplusplus-sig
> >>
> >
> >
> >
> > _______________________________________________
> > Cplusplus-sig mailing listCplusplus-sig at python.orghttp://mail.python.org/mailman/listinfo/cplusplus-sig
> >
> >
> >
> > _______________________________________________
> > Cplusplus-sig mailing list
> > Cplusplus-sig at python.org
> > http://mail.python.org/mailman/listinfo/cplusplus-sig
> >
> 


-- 
Any opinions or advice expressed here do NOT reflect those
of my employer Research In Motion Inc.
Work Portfolio: http://careers.stackoverflow.com/nialldouglas/



-------------- next part --------------
A non-text attachment was scrubbed...
Name: SMime.p7s
Type: application/x-pkcs7-signature
Size: 6061 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20121224/ac9d4861/attachment.bin>


More information about the Cplusplus-sig mailing list