- 论坛徽章:
- 0
|
本帖最后由 壮乡小顽主 于 2012-08-15 16:50 编辑
python核心编程书上的脚本如下:
import thread
from time import ctime, sleep
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, locks))
print 'to here 2'
for i in nloops:
while locks.locked():pass
print 'all DONE at:', ctime()
if __name__ == '__main__':
main()
在python2.6上运行后报错,见截图。通过打印确定问题出在thread.start_new_thread(loop, (i, loops, locks)),麻烦高手解释一下为什么,十分感谢!脚本里的[]出不来,请看截图。
|
|