public
Last active

An example of how Thread._Thread__stop() does not stop a thread

  • Download Gist
thread_stop_fail.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#! /usr/bin/python
 
import time
import threading
 
def f():
for x in range(10):
print "I am alive and running"
time.sleep(0.1)
 
 
thread = threading.Thread(target=f)
thread.start()
time.sleep(0.5)
print "Stopping the thread"
thread._Thread__stop()
print "Stopped the thread"
time.sleep(1)
 
 
# On my system this gives the following output:
# torsten@sharokan:~$ python demo.py
# I am alive and running
# I am alive and running
# I am alive and running
# I am alive and running
# I am alive and running
# Stopping the thread
# Stopped the thread
# I am alive and running
# I am alive and running
# I am alive and running
# I am alive and running
# I am alive and running

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.