Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unpreempted behavior with sys.setcheckinterval

11 views
Skip to first unread message

k3xji

unread,
Apr 16, 2009, 10:26:05 AM4/16/09
to
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()

Aahz

unread,
Apr 17, 2009, 9:23:49 AM4/17/09
to
In article <c982207b-388d-4608...@u8g2000yqn.googlegroups.com>,

k3xji <sum...@gmail.com> wrote:
>
>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.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"If you think it's expensive to hire a professional to do the job, wait
until you hire an amateur." --Red Adair

0 new messages