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

相关推荐
c***8719几秒前
Flask:后端框架使用
后端·python·flask
天硕国产存储技术站15 分钟前
DualPLP 双重掉电保护赋能 天硕工业级SSD筑牢关键领域安全存储方案
大数据·人工智能·安全·固态硬盘
百***354822 分钟前
JavaScript在Node.js中的集群部署
开发语言·javascript·node.js
雷文成.思泉软件23 分钟前
以ERP为核心、企微为门户,实现一体化集成
大数据·低代码·创业创新
光影少年24 分钟前
node.js和nest.js做智能体开发需要会哪些东西
开发语言·javascript·人工智能·node.js
xu_yule1 小时前
Linux_14(多线程)线程控制+C++多线程
java·开发语言·jvm
c***97981 小时前
PHP在内容管理中的模板引擎
开发语言·php
San30.1 小时前
深入理解 JavaScript 异步编程:从 Ajax 到 Promise
开发语言·javascript·ajax·promise
XIAOYU6720131 小时前
2026大专跨境电商专业,想好就业考哪些证书比较好?
开发语言
Q_Q5110082851 小时前
python+django/flask的情绪宣泄系统
spring boot·python·pycharm·django·flask·node.js·php