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

相关推荐
今天AI了吗13 分钟前
Spring AI 框架实战:Java 后端集成大模型的架构设计与工程落地
java·人工智能·python·spring·机器学习
code bean20 分钟前
【C#】 主构造函数(Primary Constructors)的来龙去脉
开发语言·c#
艾派森21 分钟前
Web Scraper API vs 自建爬虫:一次真实对比测试,结果让人震惊
爬虫·python·网络爬虫
小柯南敲键盘28 分钟前
AI批量翻译Temu商品标题的Python实践
开发语言·人工智能·python
逆境不可逃31 分钟前
Java JUC 同步工具类一次讲透:CountDownLatch、CyclicBarrier、Semaphore、Phaser 与 AQS 共享模式
java·开发语言·python
遥感知识服务37 分钟前
SMAP看每天有多少水,Landsat告诉水最可能出现在哪里:拆解Idai洪水数据驱动预报
python
大尚来也1 小时前
项目上线前必须检查的清单:避开线上重大事故
开发语言
微石科技1 小时前
慢病居家长期服务方案怎么选?宁波微石科技星梦云康打通居家慢病服务闭环
大数据·人工智能
酉鬼女又兒1 小时前
[特殊字符]零基础入门AI:归纳演绎、假设空间、归纳偏好、NFL、过拟合与欠拟合、模型评估选择、超参数、性能度量、混淆矩阵、P-R曲线和F1
人工智能·windows·python·深度学习·安全·机器学习·矩阵
q27551300421 小时前
AS717 Type-C转DP 8K方案外维少 AS717电路图
c语言·开发语言