3 Replies - 1378 Views - Last Post: 08 September 2014 - 10:18 PM Rate Topic: -----

#1 bobdxcool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-October 13

signal handler python threading error

Posted 07 September 2014 - 05:38 PM

I am trying to check a counter status in a certain time duration using signal handler in python. I am trying to see if the counter value increases by 1 within 3 seconds and also if it increases by 3 or more in 0.5 seconds. Then I will print some data based on this. The problem is that the program works fine while I run it in a while loop. But I want to run it as a thread, for which I get the error, "signal only works in main thread". How can I change the code to make it work with threading?


import  time, signal, os, threading

counter=0
def handler(signum, frame):
   print "counter ok"

def counteras():
  global counter

  while(1):


    hallActive=1

    if (hallActive == 1):
        #print hallActive
        counter=counter+1
        print counter
        return counter

    else:
        x=1

def counterstuck(): #function to check if counter value is increasing by 1 in 3 seconds

   signal.signal(signal.SIGALRM, handler)
   signal.alarm(3)
   stuck=counteras()
   while(counteras() > stuck):
      signal.alarm(0)
      print("counter not ok")
      return 0


def counterstuck1():#function to check if counter value is increasing by more than 3 in 0.5 seconds

  signal.signal(signal.SIGALRM, handler)
  signal.alarm(0.5)
  stuck=counteras()
  while(counteras() !=stuck+3):
      signal.alarm(0)
      print("Counter not ok")
      return 0
def main():
   counterThread= threading.Thread(target=counter)
   counterstuckThread= threading.Thread(target=counterstuck)
   counterstuckoneThread= threading.Thread(target=counterstuck1)
   counterThread.start()
   counterstuckThread.start()
   counterstuckoneThread.start()



Is This A Good Question/Topic? 0
  • +

Replies To: signal handler python threading error

#2 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,553
  • Joined: 12-December 12

Re: signal handler python threading error

Posted 07 September 2014 - 05:55 PM

This is a question, and problem that you are having, not an Advanced Discussion. Topic moved.
Was This Post Helpful? 0
  • +
  • -

#3 bobdxcool  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 27-October 13

Re: signal handler python threading error

Posted 08 September 2014 - 06:27 PM

View Postandrewsw, on 07 September 2014 - 05:55 PM, said:

This is a question, and problem that you are having, not an Advanced Discussion. Topic moved.



where has it been moved to ?
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,553
  • Joined: 12-December 12

Re: signal handler python threading error

Posted 08 September 2014 - 10:18 PM

The Python forum.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1