本文主要是介绍Python多线程 threading模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
用threading模块,可以实现Python多线程编程。
import threading
import timedef video(secs):for i in range(secs):print("边看瘦身操......%d" % i)time.sleep(1)def dance(secs):for i in range(secs):print("边跳舞...%d" % i)time.sleep(1)def drink(secs):time.sleep(secs)print("中间喝点水")threads = []
t1 = threading.Thread(target=video, args=(10,))
t2 = threading.Thread(target=dance, args=(10,))
t3 = threading.Thread(target=drink, args=(5, ))
threads.extend([t1, t2, t3])if __name__ == '__main__':print('---开始---:%s' % time.ctime())for t in threads:t.setDaemon(True)t.start()t1.join()t2.join()print('---结束---:%s' % time.ctime())
1. import threading # 导入threading模块,threading模块对thread模块(Python3中是_thread模块)做了一些封装,直接使用thr
这篇关于Python多线程 threading模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!