Unpreempted behavior with sys.setcheckinterval

K

k3xji

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()
 
A

Aahz

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.

I'm sorry, I think English is not your primary language, and I can't
quite understand what you mean here. Please rephrase.

Note that sys.setcheckinterval() is *not* designed to force threads to
run to completion, only to allow you to change the time chunk for a
thread to process, and if anything goes through the GIL, the thread may
yield. To force a thread to be the only thread running, you need to use
some kind of locking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top