Unpreempted behavior with sys.setcheckinterval
k3xji
sumerc at gmail.com
Thu Apr 16 10:26:05 EDT 2009
Hi all,
I want unpreempted behavior for some application and do some testing
as below. Well the unpreemption behavior is working fine with
sys.setcheckinterval(sys.maxint). However, when I set the interval to
a lower value, the thread does not being preempted anymore, it runs
until it is finished. The output of the below program is :
Thread 1 is printing out 2000 lines and then Thread 2 prints 2000
lines subsequently.
Here is the code:
import sys
import threading
class B(threading.Thread):
def __init__(self, tid):
threading.Thread.__init__(self)
self.cnt = 0
self.tid = tid
def run(self):
# open file as 'a' to append the line
f = open('preempt_output', 'a')
f.write('Thread '+str(self.tid)+'is starting...\n')
SetUnpreemptable(True)
while(1):
self.cnt += 1
f.write('Thread '+str(self.tid)+':'+str(self.cnt) +'\n')
if self.cnt == 200:
SetUnpreemptable(False)
if self.cnt == 2000:
break
f.close()
def SetUnpreemptable(b):
try:
if b:
sys.setcheckinterval(sys.maxint)
else:
raise
except:
sys.setcheckinterval(dflt_sysinterval)
if __name__ == "__main__":
dflt_sysinterval = sys.getcheckinterval()
thrd3 = B(1)
thrd4 = B(2)
thrd3.start()
thrd4.start()
More information about the Python-list
mailing list