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

相关推荐
小蒜学长34 分钟前
springboot二手儿童绘本交易系统设计与实现(代码+数据库+LW)
java·开发语言·spring boot·后端
MongoVIP35 分钟前
Scrapy爬虫实战:正则高效解析豆瓣电影
python·scrapy
李小白6640 分钟前
Python文件操作
开发语言·python
xqlily1 小时前
Go语言:高效简洁的现代编程语言
开发语言·后端·golang
数据知道1 小时前
Go语言:数据压缩与解压详解
服务器·开发语言·网络·后端·golang·go语言
席万里1 小时前
什么是GO语言里面的GMP调度模型?
开发语言·后端·golang
hqwest1 小时前
QT肝8天09--用户列表
开发语言·c++·qt·上位机·qt开发
weixin_525936332 小时前
金融大数据处理与分析
hadoop·python·hdfs·金融·数据分析·spark·matplotlib
Zwb2997922 小时前
Day 30 - 错误、异常与 JSON 数据 - Python学习笔记
笔记·python·学习·json
山,离天三尺三2 小时前
基于LINUX平台使用C语言实现MQTT协议连接华为云平台(IOT)(网络编程)
linux·c语言·开发语言·网络·物联网·算法·华为云