module signal

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Klaus Neuner

    #1

    module signal

    Hello,

    consider the following two programs:

    # (1)

    import sys, signal

    def alarm_handler(s ignum, frame):
    raise

    try:
    signal.signal(s ignal.SIGALRM, alarm_handler)
    signal.alarm(3)
    n = 0
    while 1:
    print n
    n = n+1
    except:
    print "Time over."

    ############### ############### ############### ############### #

    # (2)

    import sys, signal

    def alarm_handler(s ignum, frame):
    raise

    try:
    signal.signal(s ignal.SIGALRM, alarm_handler)
    signal.alarm(3)
    re.search("a((( .)*c)*d)*e", "abcdf"*20)
    except:
    print "Time over."

    ############### ############### ############### ############### #

    (1) behaves the way one would expect it to behave: It stops counting
    after about 3 seconds. (2) will stop trying to match the regexp at
    once. And this behaviour will not change if you give any other
    argument to signal.alarm().

    Why?

    Another program of mine contains code that is fairly analogous to (2)
    (or at least I thought so). Here it is:


    # (3)

    def main():
    # read in a corpus file
    # compile some regexp patterns
    # and then, for each pattern do:

    for line in corpus:
    try:
    signal.signal(s ignal.SIGALRM, alarm_handler)
    signal.alarm(10 )
    test = pattern.search( line) #!
    if not test:
    out.append(line )
    else:
    out.append(some _function(line, pattern))
    except:
    out.append(line )


    ############### ############### ############### ############### #

    The regexps pattern in code (3) use to be rather complicated. When
    code (3) gets really long lines as input, Python will be executing
    line #! for days. Why?

    What I am looking for is code with the following meaning:

    for n seconds try to do:
    # any Python code may be inserted here
    print "bla"

    When the n seconds are over, Python should print "bla". It should not
    exit the program. How can this be done?


    Klaus
  • Peter Otten

    #2
    Re: module signal

    Klaus Neuner wrote:
    [color=blue]
    > # (2)
    >
    > import sys, signal
    >
    > def alarm_handler(s ignum, frame):
    > raise
    >
    > try:
    > signal.signal(s ignal.SIGALRM, alarm_handler)
    > signal.alarm(3)
    > re.search("a((( .)*c)*d)*e", "abcdf"*20)
    > except:
    > print "Time over."
    >
    > ############### ############### ############### ############### #
    >
    > (1) behaves the way one would expect it to behave: It stops counting
    > after about 3 seconds. (2) will stop trying to match the regexp at
    > once. And this behaviour will not change if you give any other
    > argument to signal.alarm().
    >
    > Why?[/color]

    To remind you that a bare except is always a bad idea. Modify your program
    along these lines

    class Timeout(Excepti on): pass

    def alarm_handler(s ignum, frame):
    raise Timeout

    try
    ...
    except Timeout:
    print "Time over."

    to see that you didn't import the re module which raises a NameError.
    Unfortunately the resulting script does not terminate anymore and I cannot
    help you with that.

    Peter

    Comment

    • Josef Meile

      #3
      Re: module signal

      Hi Klaus,
      [color=blue]
      > What I am looking for is code with the following meaning:
      >
      > for n seconds try to do:
      > # any Python code may be inserted here
      > print "bla"
      >
      > When the n seconds are over, Python should print "bla". It should not
      > exit the program. How can this be done?[/color]
      It should work with a signal alarm; I don't know why it
      doesn't. You could also try a Timer object from the
      threading class; it's even easier than a signal alarm.

      Regards,
      Josef

      Comment

      • Klaus Neuner

        #4
        Re: module signal

        > to see that you didn't import the re module which raises a NameError.

        O.k. Sorry for that. Sometimes one doesn't see the most obvious things.

        So I should reformulate my problem as follows:

        Why does (2) not work (though (1) does)?

        # (1)

        import sys, signal


        class Timeout(Excepti on):
        pass

        def alarm_handler(s ignum, frame):
        raise Timeout

        try:
        signal.signal(s ignal.SIGALRM, alarm_handler)
        signal.alarm(3)
        n = 0
        while 1:
        print n
        n = n+1
        except Timeout:
        print "Time over."

        ############### ############### ############### ############### #

        # (2)

        import sys, signal, re

        class Timeout(Excepti on):
        pass

        def alarm_handler(s ignum, frame):
        raise Timeout

        try:
        signal.signal(s ignal.SIGALRM, alarm_handler)
        signal.alarm(3)
        re.search("a((( .)*c)*d)*e", "abcdf"*20)
        except Timeout:
        print "Time over."

        Comment

        • Klaus Neuner

          #5
          Re: module signal

          > doesn't. You could also try a Timer object from the[color=blue]
          > threading class; it's even easier than a signal alarm.[/color]

          I don't see how to do it with a Timer object only. Could you rewrite
          the regexp example with a Timer object such that it works?

          Klaus

          Comment

          • Josef Meile

            #6
            Re: module signal

            > I don't see how to do it with a Timer object only. Could you rewrite[color=blue]
            > the regexp example with a Timer object such that it works?[/color]
            Oh yes, I'm wrong. I confused this module with something else, sorry.

            Comment

            Working...