thread wierdness

anton wilson anton.wilson at camotion.com
Fri Jul 26 14:12:27 EDT 2002


I'm trying to create a simple thread with the following code:
============================================
import time, thread, thr_test, sys


def do_computations(str):
    thr_test.set_priority("") #set the priority of this thread really high
    compute()


def compute():
    i = 0
    x = 0
    while i < 1000:
        for y in range(10000):
            x = x + 1
            x = x - 1
        i = i + 1
        i = i * 1


sys.setcheckinterval(2000)

thr_test.set_priority("") #set the priority of main thread really high
thread.start_new(do_computations, ("",))

sys.setcheckinterval(10)
compute()
========================================


I spawn 1 additional python thread
and then both of them could do compute.


The following printouts from within the kernel scheduler itself show the 
process that was running on the CPU at the time the schduler was run, the new 
process to chosen to run next, and the time that the change was made in 
microseconds. 

Instead of the two high-priority threads I thought I created, I see 3 threads 
with the PIDs 1374, 1375, and 1376. 

Additionally there is a huge gap in time (multiple seconds) on the 6th line 
when process 1374 was scheduled and the 7th line when it was removed from the 
CPU. This breaks my assumption that python threads will switch every 10 byte 
codes. Does anyone have any suggested explanations?

[(1374) (1375) - 778582]
[(1375) (1374) - 778598]
[(1374) (1375) - 778618]
[(1375) (1376) -778678]
[(1376) (1375) - 778696]
[(1375) (1374) - 778713]

[(1374) (1376) - 506072]
[(1376) (1375) - 506107]
[(1375) (1376) - 506150]
[(1376) (1375) - 506184]
[(1375) (1374) - 506213]
[(1374) 1068 - 506496]




More information about the Python-list mailing list