Interrupting a blocking function frolm another thread.

superhac007 at gmail.com superhac007 at gmail.com
Sun Apr 8 17:52:51 EDT 2012


On Sunday, April 8, 2012 3:55:41 PM UTC-5, Adam Skutt wrote:
> On Apr 8, 2:45 pm, "superhac... at gmail.com" <superhac... at gmail.com>
> wrote:
> > I am using the python module nfqueue-bindings which is a nfqueue
> > packet intercepting module.  It uses the following snippet of code to
> > start the process:
> >
> > print "trying to run"
> > try:
> >      q.try_run()
> >      except KeyboardInterrupt, e:
> >      print "interrupted"
> >
> > The q.try_run() method blocks.   I would like to be able to interrupt
> > this the same way you can hit control-c to unblock it, but from
> > another thread.  Does anyone have any idea on how to do this?  Is
> > there some sort of Exception I can generate from another thread that
> > would do this?
> 
> The simplest and most reliable way will be to modify the asynchronous
> I/O example at https://www.wzdftpd.net/redmine/projects/nfqueue-bindings/repository/entry/examples/nfq_asyncore.py
> to do what you want.  The classical way to accomplish this would be to
> create a second I/O descriptor (via os.pipe or similiar) and wait for
> that descriptor in the same async I/O loop.  When you want to stop the
> loop, send a message (could be a single byte) to the second descriptor
> and then respond appropriately in your asynchronous I/O handler.
> 
> However, simply ignoring the nfqueue socket while doing other
> processing might be acceptable too.  I would read the documentation
> and ask questions carefully before taking that approach, as it may
> lead to lost data or other problems.  The viability of this approach
> depends heavily on your application.
> 
> Adam

Thanks for the reply Adam.  I am still confused with the example with the example you linked to.  I have only been working with Python for a couple of weeks, but I am fluent with C.  Given the example you linked to, are you saying that this is the run loop for asynchronous comm that a end user of the module could use?  Or is this something with the nfqueue-bindings module that I would I have to modify.  I don't recognize the the main loop in the example.  

I am not not worried about losing data, I just need to start and stop this from another thread.




More information about the Python-list mailing list