python线程threading

线程示例:

import threading
import time
# 唱歌任务
def sing():
    # 扩展: 获取当前线程
    # print("sing当前执行的线程为:", threading.current_thread())
    for i in range(5):
        print("正在唱歌...%d" % i)
        time.sleep(1)

# 跳舞任务
def dance():
    # 扩展: 获取当前线程
    # print("dance当前执行的线程为:", threading.current_thread())
    for i in range(5):
        print("正在跳舞...%d" % i)
        time.sleep(1)

if __name__ == '__main__':
    # 扩展: 获取当前线程
    #print("当前执行的线程为:", threading.current_thread())
    # 创建唱歌的线程
    # target: 线程执行的函数名
    sing_thread = threading.Thread(target=sing)

    # 创建跳舞的线程
    dance_thread = threading.Thread(target=dance)

    # 开启线程
    sing_thread.start()
    dance_thread.start()
    sing_thread.join()
    dance_thread.join()

  

参考:

https://blog.csdn.net/hlg1995/article/details/81568773

https://blog.csdn.net/sinat_38682860/article/details/102551070

posted @ 2020-01-28 18:20  风云_就是她了  阅读(206)  评论(0编辑  收藏  举报