Python多线程的执行

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import time
import threading
 
def music():
    for i in range(3):
        print("听音乐。。。")
        time.sleep(0.5)
 
def coding():
    for i in range(3):
        print("敲代码。。。")
        time.sleep(0.5)
 
 
if __name__ == '__main__':
    start = time.time()
    music_thread = threading.Thread(target=music)
    coding_thread = threading.Thread(target=coding)
 
    music_thread.start()
    coding_thread.start()
 
    music_thread.join()
    coding_thread.join()
 
    end = time.time()
    print(f'程序执行了{end - start}秒')

  

结果如下:无序

 

1
2
3
4
5
6
7
听音乐。。。
敲代码。。。
听音乐。。。敲代码。。。
 
敲代码。。。
听音乐。。。
程序执行了1.5029683113098145

  

posted @   宝山方圆  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2017-04-17 pyspark采用python3开发
点击右上角即可分享
微信分享提示