python 多线程 thread

import thread
def loop0():
    print "loop0 start at:",ctime()
    sleep(4)
    print "loop0 end at:",ctime()

def loop1():
    print "loop1 start at:",ctime()
    sleep(2)
    print "loop1 end at:",ctime()


def main():
    print "main start at:",ctime()
    thread.start_new_thread(loop0,())
    thread.start_new_thread(loop1,())
    sleep(4)
    # loop0()
    # loop1()
    print "all end at:",ctime()

if __name__ == '__main__':
    main()

执行后:
main start at: Wed Jan 20 18:17:05 2016
loop0 start at: Wed Jan 20 18:17:05 2016
loop1 start at: Wed Jan 20 18:17:05 2016
loop1 end at: Wed Jan 20 18:17:07 2016
all end at: Wed Jan 20 18:17:09 2016
Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr

如果用python的thread,则loop0没有执行完成,则执行主线程了,所以不推荐使用

你可能感兴趣的:(Python)