月度归档:2012年12月

python thread测试问题

一个朋友写的测试,出了点小问题,原来是最后一句多了空格…这不是关键。关键输出会乱掉:

>>>
Starting at: Thu Dec 06 21:24:38 2012
start loopstart loop 01 at:at: Thu Dec 06 21:24:38 2012Thu Dec 06 21:24:38 2012

loop 1 Done at: Thu Dec 06 21:24:40 2012
loop 0 Done at: Thu Dec 06 21:24:42 2012
All Done at: Thu Dec 06 21:24:42 2012

后来我到单核机器测试,输出就ok:

>>> main()
Starting at: Thu Dec 6 14:55:37 2012
start loop 0 at: Thu Dec 6 14:55:37 2012
start loop 1 at: Thu Dec 6 14:55:37 2012
loop 1 Done at: Thu Dec 6 14:55:39 2012
loop 0 Done at: Thu Dec 6 14:55:41 2012
All Done at: Thu Dec 6 14:55:41 2012
>>> main()
Starting at: Thu Dec 6 14:56:07 2012
start loop 0 at: Thu Dec 6 14:56:07 2012
start loop 1 at: Thu Dec 6 14:56:07 2012
loop 1 Done at: Thu Dec 6 14:56:09 2012
loop 0 Done at: Thu Dec 6 14:56:11 2012
All Done at: Thu Dec 6 14:56:11 2012
>>>

于是我判断可能是多核机器就有这个问题。

 

然后我将print语句全部加上括号,则本机显示正常了:

>>>
Starting at: Thu Dec 06 21:42:29 2012
(‘start loop’, 0, ‘at:’, ‘Thu Dec 06 21:42:29 2012′)(‘start loop’, 1, ‘at:’, ‘Thu Dec 06 21:42:29 2012′)

(‘loop’, 1, ‘Done at:’, ‘Thu Dec 06 21:42:31 2012′)
(‘loop’, 0, ‘Done at:’, ‘Thu Dec 06 21:42:33 2012′)
(‘All Done at:’, ‘Thu Dec 06 21:42:33 2012′)
>>> ================================ RESTART ================================
>>>
Starting at: Thu Dec 06 21:45:20 2012
(‘start loop’, 0, ‘at:’, ‘Thu Dec 06 21:45:20 2012′)(‘start loop’, 1, ‘at:’, ‘Thu Dec 06 21:45:20 2012′)

(‘loop’, 1, ‘Done at:’, ‘Thu Dec 06 21:45:23 2012′)
(‘loop’, 0, ‘Done at:’, ‘Thu Dec 06 21:45:25 2012′)
(‘All Done at:’, ‘Thu Dec 06 21:45:25 2012′)
>>> ================================ RESTART ================================
>>>
Starting at: Thu Dec 06 21:45:29 2012
(‘start loop’, 0, ‘at:’, ‘Thu Dec 06 21:45:29 2012′)(‘start loop’, 1, ‘at:’, ‘Thu Dec 06 21:45:29 2012′)

(‘loop’, 1, ‘Done at:’, ‘Thu Dec 06 21:45:31 2012′)
(‘loop’, 0, ‘Done at:’, ‘Thu Dec 06 21:45:33 2012′)
(‘All Done at:’, ‘Thu Dec 06 21:45:33 2012′)

 

但在一台linux服务器上,还不正常:

>>> main()
Starting at: Thu Dec 6 06:46:24 2012
(‘start loop’, (‘start loop’, 10, , ‘at’at::”, ‘Thu Dec 6 06, :46:24 2012”Thu Dec 6 06:46:24 2012′))

(‘loop’, 1, ‘Done at:’, ‘Thu Dec 6 06:46:26 2012′)
(‘loop’, 0, ‘Done at:’, ‘Thu Dec 6 06:46:28 2012′)
(‘All Done at:’, ‘Thu Dec 6 06:46:28 2012′)
>>> main()
Starting at: Thu Dec 6 06:46:30 2012
(‘start loop'(‘start loop’, , 10, , ‘at:”at:’, ‘Thu Dec 6 06:46:30 2012′),
‘Thu Dec 6 06:46:30 2012′)
(‘loop’, 1, ‘Done at:’, ‘Thu Dec 6 06:46:32 2012′)
(‘loop’, 0, ‘Done at:’, ‘Thu Dec 6 06:46:34 2012′)
(‘All Done at:’, ‘Thu Dec 6 06:46:34 2012′)

 

代码:

import thread
from time import sleep,ctime
loops=[4,2]

def loop(nloop,nsec,lock):
print (‘start loop’,nloop,’at:’,ctime())
sleep(nsec)
print (‘loop’,nloop,’Done at:’,ctime())
lock.release()

def main():
print ‘Starting at:’,ctime()
locks=[]
nloops = range(len(loops))

for i in nloops:
lock =thread.allocate_lock()
lock.acquire()
locks.append(lock)

for i in nloops:
thread.start_new_thread(loop,(i,loops[i],locks[i]))

for i in nloops:
while locks[i].locked():pass

print (‘All Done at:’,ctime())

if __name__== ‘__main__':
main()