Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #16140 > unrolled thread

Bind key press to call function

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2011-11-24 03:55 +0000
Last post2011-11-24 17:20 +1100
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Bind key press to call function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-24 03:55 +0000
    Re: Bind key press to call function Chris Angelico <rosuav@gmail.com> - 2011-11-24 15:20 +1100
      Re: Bind key press to call function Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-24 06:07 +0000
        Re: Bind key press to call function Chris Angelico <rosuav@gmail.com> - 2011-11-24 17:20 +1100

#16140 — Bind key press to call function

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-11-24 03:55 +0000
SubjectBind key press to call function
Message-ID<4ecdc04b$0$30003$c3e8da3$5496439d@news.astraweb.com>
I'm looking for a way to interrupt a long-running function on a key 
press, but without halting the function.

E.g. if I have these two functions:

def handler(*args):
    print "caught interrupt and continuing..."


def exercise_cpu():
    for i in range(8):
        print "working..."
        for j in range(1000000):
            pass
    print "done"


and I call exercise_cpu(), then type some key combination (say, Ctrl-x-p 
for the sake of the argument), I'd like the result to look something like 
this:

>>> exercise_cpu()
working...
working...
working...
working...
working...
working...
caught interrupt and continuing...
working...
working...
done


I think I want to use the readline module to catch the key press Ctrl-x-p 
and generate a signal, say SIGUSR1, then use the signal module to install 
a signal handler to catch SIGUSR1. Is this the right approach, or is 
there a better one? Does anyone show me an example of working code that 
does this?

Linux only solutions are acceptable.


-- 
Steven

[toc] | [next] | [standalone]


#16143

FromChris Angelico <rosuav@gmail.com>
Date2011-11-24 15:20 +1100
Message-ID<mailman.2988.1322108411.27778.python-list@python.org>
In reply to#16140
On Thu, Nov 24, 2011 at 2:55 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> I'm looking for a way to interrupt a long-running function on a key
> press, but without halting the function.

I assume there's a reason for not using Ctrl-C and SIGINT with the
signal module?

This looks like the classic "sigint handler sets a flag that the main
loop polls" structure.

ChrisA

[toc] | [prev] | [next] | [standalone]


#16146

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-11-24 06:07 +0000
Message-ID<4ecddf33$0$30003$c3e8da3$5496439d@news.astraweb.com>
In reply to#16143
On Thu, 24 Nov 2011 15:20:09 +1100, Chris Angelico wrote:

> On Thu, Nov 24, 2011 at 2:55 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> I'm looking for a way to interrupt a long-running function on a key
>> press, but without halting the function.
> 
> I assume there's a reason for not using Ctrl-C and SIGINT with the
> signal module?

Yes, I want to leave Ctrl-C alone and have a similar, but separate, 
signal handler (or equivalent).

> This looks like the classic "sigint handler sets a flag that the main
> loop polls" structure.

Exactly. I am open to alternative methods if they are lightweight. 



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#16148

FromChris Angelico <rosuav@gmail.com>
Date2011-11-24 17:20 +1100
Message-ID<mailman.2992.1322115633.27778.python-list@python.org>
In reply to#16146
On Thu, Nov 24, 2011 at 5:07 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>> This looks like the classic "sigint handler sets a flag that the main
>> loop polls" structure.
>
> Exactly. I am open to alternative methods if they are lightweight.

Might be easiest to spin off a thread to do the work, and then have
the main thread block on the keyboard.

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web