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

相关推荐
用户25191624271122 分钟前
Python之语言特点
python
阿里云大数据AI技术44 分钟前
StarRocks 助力数禾科技构建实时数仓:从数据孤岛到智能决策
大数据
刘立军44 分钟前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
数据智能老司机4 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
Lx3525 小时前
Hadoop数据处理优化:减少Shuffle阶段的性能损耗
大数据·hadoop
数据智能老司机5 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i7 小时前
django中的FBV 和 CBV
python·django
c8i7 小时前
python中的闭包和装饰器
python
武子康9 小时前
大数据-99 Spark Streaming 数据源全面总结:原理、应用 文件流、Socket、RDD队列流
大数据·后端·spark
这里有鱼汤10 小时前
小白必看:QMT里的miniQMT入门教程
后端·python