首页 > 开发 > Python > 正文

Python 多线程运行,可是为什么是Thread-2先执行

2017-09-06 19:11:33  来源:网友分享
#!/usr/bin/python# -*- coding: UTF-8 -*-import threadimport timedef print_time(threadName, delay, count):    print "%s: %s  %s \n" % (threadName, time.ctime(time.time()), str(count))    count = count    while count < 5:        time.sleep(delay)        count += 1        print "%s: %s  %s" % (threadName, time.ctime(time.time()), str(count))try:    thread.start_new_thread(print_time, ("Thread-1", 1, 0))    thread.start_new_thread(print_time, ("Thread-2", 3, 0))except:    print "Error: unable to start thread"while 1:    pass

运行结果:

解决方案

随机的,但是python不推荐thread模块,建议用threading