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


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

Capturing SIGSTOP

Started bySteven D'Aprano <steve+comp.lang.python@pearwood.info>
First post2011-11-24 03:29 +0000
Last post2011-11-24 19:15 +1100
Articles 5 — 2 participants

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


Contents

  Capturing SIGSTOP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-24 03:29 +0000
    Re: Capturing SIGSTOP Chris Angelico <rosuav@gmail.com> - 2011-11-24 15:22 +1100
      Re: Capturing SIGSTOP Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-11-24 06:36 +0000
        Re: Capturing SIGSTOP Chris Angelico <rosuav@gmail.com> - 2011-11-24 19:12 +1100
        Re: Capturing SIGSTOP Chris Angelico <rosuav@gmail.com> - 2011-11-24 19:15 +1100

#16139 — Capturing SIGSTOP

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-11-24 03:29 +0000
SubjectCapturing SIGSTOP
Message-ID<4ecdba0c$0$30003$c3e8da3$5496439d@news.astraweb.com>
I'd like to perform a task when the user interrupts my application with 
Ctrl-Z on Linux. I tried installing a signal handler:


import signal

def handler(signalnum, stackframe):
    print "Received signal %d" % signalnum

signal.signal(signal.SIGSTOP, handler)

But I got a RuntimeError:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
RuntimeError: (22, 'Invalid argument')


This isn't documented:

http://docs.python.org/library/signal.html#signal.signal


Is there a way to catch SIGSTOP?



-- 
Steven

[toc] | [next] | [standalone]


#16144

FromChris Angelico <rosuav@gmail.com>
Date2011-11-24 15:22 +1100
Message-ID<mailman.2989.1322108547.27778.python-list@python.org>
In reply to#16139
On Thu, Nov 24, 2011 at 2:29 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> Is there a way to catch SIGSTOP?

In the strictest sense, no; SIGSTOP can't be caught. However, some
systems have SIGTSTP which is sent when you hit Ctrl-Z, which would be
what you're looking for.

http://www.gnu.org/s/hello/manual/libc/Job-Control-Signals.html

Tested on my Linux box only; this almost certainly won't work on Windows.

ChrisA

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


#16150

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

> On Thu, Nov 24, 2011 at 2:29 PM, Steven D'Aprano
> <steve+comp.lang.python@pearwood.info> wrote:
>> Is there a way to catch SIGSTOP?
> 
> In the strictest sense, no; SIGSTOP can't be caught. However, some
> systems have SIGTSTP which is sent when you hit Ctrl-Z, which would be
> what you're looking for.

That's exactly what I'm looking for, thanks.

After catching the interrupt and doing whatever I need to do, I want to 
allow the process to be stopped as normal. Is this the right way?

import signal, os

def handler(signalnum, stackframe):
    print "Received signal %d" % signalnum
    os.kill(os.getpid(), signal.SIGSTOP)  # Hit myself with a brick.

signal.signal(signal.SIGTSTP, handler)


It seems to work for me (on Linux), but is it the right way?



-- 
Steven

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


#16154

FromChris Angelico <rosuav@gmail.com>
Date2011-11-24 19:12 +1100
Message-ID<mailman.2997.1322122336.27778.python-list@python.org>
In reply to#16150
On Thu, Nov 24, 2011 at 5:36 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>    os.kill(os.getpid(), signal.SIGSTOP)  # Hit myself with a brick.
>

Sometimes there'll be a raise() function but it's going to do the same
thing. Yep, that would be the way to do it.

ChrisA

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


#16155

FromChris Angelico <rosuav@gmail.com>
Date2011-11-24 19:15 +1100
Message-ID<mailman.2998.1322122505.27778.python-list@python.org>
In reply to#16150
On Thu, Nov 24, 2011 at 5:36 PM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
>    os.kill(os.getpid(), signal.SIGSTOP)  # Hit myself with a brick.
>
>
> It seems to work for me (on Linux), but is it the right way?

And - if your system has SIGTSTP, it'll have SIGSTOP and this will be
how it works. (Windows has neither.) This code will probably work fine
on all modern Unix-like systems, but if it fails anywhere, it'll be
for lack of SIGTSTP I would say.

ChrisA

[toc] | [prev] | [standalone]


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


csiph-web