4

I am having a main program which is defined like this:

main.py

def main():

try:
    registry.start_server()
except:
    print("Shutting down the program")
    pass


if __name__ == '__main__':
    main()

registry.start_server() is the method in another module which looks like this:

def start_server():
    t_server = threading.Thread(target=server.start)
    t_server.start()
    try:
        t_server.join()
    except KeyboardInterrupt:
        print("Error")
        raise ValueError

finally:
    fp.close()

server.start is the method in another module which does some listening work in a while(True) manner. I am not sure how to stop the whole program when clicking Stop in PyCharm which is Ctrl + C (Signal). I tried with Event but without success. I get to the main.py by raising an exception when the signal gets caught but that does not terminate the whole program. It shows Waiting for program to detach. The only way is to use SIGKILL. I don't understand where does the program keeps hanging? I have also tried calling sys.exit(0) when the signal gets caught and creating the thread as Deamon but that didnt help either.

EDIT While True method in another module

def start(self, event):

 try:
        while True:
            if event.is_set():
                if self.pubsub.channels:
                    print("It enters here")
                    message = self.pubsub.get_message(True)
                    if message:
                          . 
                          .
                          .
           else:
                return
Pegasus Thorn
  • 159
  • 1
  • 2
  • 12
  • *"work in a while(True) manner"*: How did you expect to **end** this infinit loop? Read [close a thread on multithreading](https://stackoverflow.com/a/43686996/7414759) – stovfl Feb 24 '19 at 11:36
  • It breaks out of while loop now, however I am still not able to catch KeyboardInterrupt Exception. I have read all the answers on SO which use thread.join or signal.pause() but that will block main thread (it get stuck on checking if the event.is_set() in the while loop). I have also tried adding atexit and setting event.clear and sys.exit(0) but I still get KI Exception – Pegasus Thorn Feb 24 '19 at 13:48
  • 1
    Try this approach [KeyboardInterrupt multiple threads at once](https://stackoverflow.com/a/50886974/7414759) – stovfl Feb 24 '19 at 14:07
  • I don't understand why the mehtod which is called in another thread is not being executed normally when I have any kind of waiting process inside main thread. e.g t.join() or time.sleep(1) after t.start(). It immediately returns to the main thread and ignores the method in another thread. – Pegasus Thorn Feb 24 '19 at 15:04
  • 1
    *"I don't understand why"*: **Without** a [mcve] I couldn't tell anything? – stovfl Feb 24 '19 at 15:24
  • See my edit. Whenever I put any while loop after t_server.start() this thread has the control and "It enteres here" is never printed. The *if event_is_set()* get executed but the program won't continue to work normally and print next statement – Pegasus Thorn Feb 24 '19 at 15:36
  • I was trying to put the main thread to sleep or deamon but since it does not have anything to do deamon mode is not very useful. It terminates immediately. I would like to catch Keyboard Interrupt and then set *event.clear()* and *t_server.join()*, but it seems impossible to do it – Pegasus Thorn Feb 24 '19 at 15:39
  • **BAD**: if `def self(...` is part of a `class xy(Thread)`. **BAD**: `try: ... except ...` enclosing a bunch of code lines. We have reached end of comments. – stovfl Feb 24 '19 at 15:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188954/discussion-between-pegasus-thorn-and-stovfl). – Pegasus Thorn Feb 24 '19 at 16:13

1 Answers1

0

To solve the problem, all you need to do is:

  1. let the child-thread exit, and
  2. let main thread join the child-thread.
seedjyh
  • 351
  • 3
  • 5