python-多线程(笔记)(持续更新)

多线程

python 复制代码
import threading
import time


def sing():
    while True:
        print("sing a song")
        time.sleep(1)

def dance():
    while True:
        print("chang tiao rap")
        time.sleep(1)

if __name__ =='__main__':

  # 此时只能执行一个函数,必须等到该函数执行完才行
    # sing()
    # dance()
 # 改进 同时进行
    tread01=threading.Thread(target=sing)
    tread02=threading.Thread(target=dance)
    tread01.start()
    tread02.start()
python 复制代码
import threading
import time


def sing(msg):
    while True:
        print("sing a song"+msg)
        time.sleep(1)

def dance(msg):
    while True:
        print("chang tiao rap"+msg)
        time.sleep(1)

if __name__ =='__main__':

    # sing()
    # dance()

    tread01=threading.Thread(target=sing,args=("googd",))
    # 把字典的值传进去
    tread02=threading.Thread(target=dance,kwargs={"msg":"good"})
    tread01.start()
    tread02.start()

使用Process完成多进程

python 复制代码
import multiprocessing
import threading
import time


def sing():
    while True:
        print("sing a song")
        time.sleep(1)

def dance(msg):
    while True:
        print("chang tiao rap"+msg)
        time.sleep(1)

if __name__ =='__main__':

    # sing()
    # dance()

    # 不变
    tread01=multiprocessing.Process(target=sing)

    tread02=multiprocessing.Process(target=dance,kwargs={"msg":"good"})
    tread01.start()
    tread02.start()
python 复制代码
import multiprocessing
import threading
import time


def sing():
        print("sing a song")
        yield


if __name__ =='__main__':

    g=sing()
    print(type(sing()))
    print(type(g))

<class 'generator'>
<class 'generator'>

可以把yield类比成一个return

相关推荐
yhdata几秒前
2026年生物塑料包装行业产业链分析报告
大数据·人工智能
人道领域2 分钟前
【零基础学java】(等待唤醒机制,线程池补充)
java·开发语言·jvm
ws2019073 分钟前
技术革新与生态融合:AUTO TECH China 2026广州汽车电子展如何定义行业新坐标?
大数据·人工智能·科技·汽车
智算菩萨5 分钟前
【Python自然语言处理】基于NLTK库的英文文本词频统计系统实现原理及应用
开发语言·python·自然语言处理
百胜软件@百胜软件6 分钟前
喜讯|百胜软件荣膺“2025年度零售科技最佳服务商”
大数据·人工智能
鲨莎分不晴7 分钟前
大数据的“数字金库”:HDFS 核心原理与操作指令全解
大数据·hadoop·hdfs
摩西蒙8 分钟前
阿里云 MaxCompute(原 ODPS)定时任务查询库存快照场景
java·大数据·sql·database
superman超哥8 分钟前
Rust 异步并发核心:tokio::spawn 与任务派发机制深度解析
开发语言·rust·编程语言·rust异步并发核心·rust任务派发机制
喵星人工作室9 分钟前
C++传说:神明之剑0.2.1
开发语言·c++·游戏
黎雁·泠崖10 分钟前
Java入门之吃透基础语法:注释+关键字+字面量+变量全解析
java·开发语言·intellij-idea·intellij idea