Cleanly exiting multi thread application on SIGINT

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

    #1

    Cleanly exiting multi thread application on SIGINT

    Is the following the most elegant way to exit a multi-threaded
    application on a Ctrl-C? I am a complete beginner and would have
    thought there was some way of doing it without having to use while 1:
    pass, but have yet to find a way.

    N.B. exit() is a method for cleanly exiting the thread using a queue.

    Many thanks

    Jon

    def main:
    wt = workerThread()
    wt.setDaemon(Tr ue)
    wt.start()
    ct = counterThread()
    ct.setDaemon(Tr ue)
    ct.start()
    try:
    while 1:
    pass
    except KeyboardInterru pt:
    wt.exit()
    ct.exit()

  • Fredrik Lundh

    #2
    Re: Cleanly exiting multi thread application on SIGINT

    "jrpfinch" wrote:
    Is the following the most elegant way to exit a multi-threaded
    application on a Ctrl-C? I am a complete beginner and would have
    thought there was some way of doing it without having to use while 1:
    pass, but have yet to find a way.
    def main:
    wt = workerThread()
    wt.setDaemon(Tr ue)
    wt.start()
    ct = counterThread()
    ct.setDaemon(Tr ue)
    ct.start()
    try:
    while 1:
    pass
    you could at least insert a time.sleep(1), to avoid busy-looping.

    </F>



    Comment

    • skip@pobox.com

      #3
      Re: Cleanly exiting multi thread application on SIGINT


      JonIs the following the most elegant way to exit a multi-threaded
      Jonapplication on a Ctrl-C? I am a complete beginner and would have
      Jonthought there was some way of doing it without having to use while
      Jon1: pass, but have yet to find a way.

      I thought there was some sort of wait() method in the threading module, but
      I was mistaken. Maybe:

      while threading.activ eCount():
      time.sleep(0.01 )

      ?

      Skip

      Comment

      Working...