python_day17_多线程

threading模块

python 复制代码
import time


def sing():
    while True:
        print("唱歌~~~~~~~~~~~~")
        time.sleep(1)


def dance():
    while True:
        print("跳舞############")
        time.sleep(1)
if __name__ == '__main__':
    sing()
    dance()

此时为单线程

python 复制代码
import threading
import time


def sing():
    while True:
        print("唱歌~~~~~~~~~~~~")
        time.sleep(1)


def dance():
    while True:
        print("跳舞############")
        time.sleep(1)

if __name__ == '__main__':
    # sing()
    # dance()
    # 多线程
    sing_thread = threading.Thread(target=sing)
    dance_thread = threading.Thread(target=dance)
    sing_thread.start()
    dance_thread.start()

使用threading模块,实现多线程

python 复制代码
import threading
import time


def sing_msg(msg):
    while True:
        print(msg)
        time.sleep(1)


def dance_msg(msg):
    while True:
        print(msg)
        time.sleep(1)

if __name__ == '__main__':
    # 注意此处元组
    sing_msg_thread = threading.Thread(target=sing_msg, args=("唱歌",))
    dance_msg_thread = threading.Thread(target=dance_msg, kwargs={"msg": "跳舞"})
    sing_msg_thread.start()
    dance_msg_thread.start()

元组中仅含一个元素时,需加逗号,多线程传参

相关推荐
0***1423 分钟前
JavaScript视频处理案例
开发语言·javascript·音视频
ceclar12333 分钟前
C#常用集合的使用
开发语言·windows·c#
z***I39439 分钟前
PHP Composer
开发语言·php·composer
1***81531 小时前
Swift在服务端开发的可能性探索
开发语言·ios·swift
2501_941879811 小时前
Python在微服务高并发异步流量控制与动态限流熔断架构中的实践
java·开发语言
CNRio1 小时前
ZUC国密算法深度研究:原理、实现与应用(Python、Rust)
python·算法·rust
zero13_小葵司1 小时前
JavaScript性能优化系列(八)弱网环境体验优化 - 8.2 离线支持:Service Worker实现基本离线功能
开发语言·javascript·性能优化
S***H2831 小时前
Swift在系统级应用中的开发
开发语言·ios·swift
J***Q2921 小时前
Kotlin DSL开发技巧
android·开发语言·kotlin
E***U9452 小时前
Kotlin注解处理器
java·开发语言·kotlin