May-31-2017, 07:02 AM
I have used Python thread so far, but I had a question that I can not find an answer by myself.
Google some related information, but I do not satisfy with this. Do you have any experience with this?
I have a thread as below (file name test_thread.py)
When I run this thread on Linux by invoking python test_thread.py, after 5 seconds, message "aEvent set already" is printed.
After that, there is nothing because aEvent is set once in my program. It is normal.
However, I want to know the below.
1. If I stop the test_thread.py by pressing Ctrl+C or Ctrl+Z, is the AThread really killed?
I am not confident to say AThread is killed because test_thread.py still occur when I use "top" command on Linux terminal.
2. Is there any way to "stop" the thread? Where should we put a "stop" method to stop the thread in test_thread.py?
Because I see we call "start()" method to start the thread, but there is no method to "stop" the thread.
I see there is "join()" method can terminate a thread but I do not understand how to use it.
Google some related information, but I do not satisfy with this. Do you have any experience with this?
I have a thread as below (file name test_thread.py)
import time def AThread(aEvent): while True: aEvent.wait() aEvent.clear() print "aEvent set already" aEvent = threading.Event() AThread = threading.Thread(name='AThread', target=AThread, args=(aEvent,)) try: AThread.start() except: print "Can not start AThread" time.sleep(5) aEvent.set()I have a thread named "AThread". This thread must wait for "aEvent" until "aEvent" is set.
When I run this thread on Linux by invoking python test_thread.py, after 5 seconds, message "aEvent set already" is printed.
After that, there is nothing because aEvent is set once in my program. It is normal.
However, I want to know the below.
1. If I stop the test_thread.py by pressing Ctrl+C or Ctrl+Z, is the AThread really killed?
I am not confident to say AThread is killed because test_thread.py still occur when I use "top" command on Linux terminal.
2. Is there any way to "stop" the thread? Where should we put a "stop" method to stop the thread in test_thread.py?
Because I see we call "start()" method to start the thread, but there is no method to "stop" the thread.
I see there is "join()" method can terminate a thread but I do not understand how to use it.