【20-9-22】Python实现多进程多线程

简介

对于计算机来说,有两种实现多任务的方式:并行和并发

  • 并发:一段时间内交替执行某些任务。如单核CPU轮流执行一些程序

  • 并行:一段时间内同时运行多个任务。多核cpu处理多任务

1.进程

程序分配资源的最小单位。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JptDPS0k-1600777354149)(https://secure-static.wolai.com/static/5oECQ9kaTY5K1GMQ4f5Afz/image.png)]

1.1 多进程的创建过程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jXYoDJfN-1600777354154)(https://secure-static.wolai.com/static/wS6Cf93Kph8jh5FFa9bh1J/image.png)]

1.2 示例:多进程无参数

import multiprocessing
import time

'''
# 创建子进程
sing_process = multiprocess.Process(target=sing)
# 启动子进程
sing_process.start()
'''

def dance():
    for i in range(3):
        print("跳舞")
        time.sleep(0.5)

def sing():
    for i in range(3):
        print("唱歌")
        time.sleep(0.5)

if __name__ == "__main__":
    print("单进程顺序执行")
    sing()
    dance()
    print("多进程")
    sing_process = multiprocessing.Process(target=sing)
    dance_process = multiprocessing.Process(target=dance)

    sing_process.start()
    dance_process.start()

1.3 多进程带参数

import multiprocessing
import time

'''
# 创建子进程 元祖的方式传递参数
sing_process = multiprocess.Process(target=sing,args=(3,))
# 启动子进程
sing_process.start()

# 字典的方式传递参数

'''

def dance(nums,names):
    for i in range(nums):
        print(names+"跳舞")
        time.sleep(0.5)

def sing(nums,names):
    for i in range(nums):
        print(names+"唱歌")
        time.sleep(0.5)

if __name__ == "__main__":
    print("多进程")
    sing_process = multiprocessing.Process(target=sing,args=(5,"小米"))
    dance_process = multiprocessing.Process(target=dance,kwargs={"names":"小茗","nums":6})


    sing_process.start()
    dance_process.start()

1.4 获取进程编号

import multiprocessing
import time
import os


'''
import os
print("work进程编号",os.getpid())
'''

def dance(nums,names):
    print("dance进程id:"+str(os.getpid()))
    print("dance父进程id:"+str(os.getppid()))
    for i in range(nums):
        print(names+"跳舞")
        time.sleep(0.5)

def sing(nums,names):
    print("sing进程id:"+str(os.getpid()))
    print("sing进程父id:"+str(os.getppid()))
    for i in range(nums):
        print(names+"唱歌")
        time.sleep(0.5)

if __name__ == "__main__":
    print("多进程")
    sing_process = multiprocessing.Process(target=sing,args=(5,"小米"))
    dance_process = multiprocessing.Process(target=dance,kwargs={"names":"小茗","nums":6})

    print("主进程id:"+str(os.getpid()))
    sing_process.start()
    dance_process.start()

1.5 守护进程

import time
import multiprocessing

def work():
    for i in range(10):
        print("工作中...")
        time.sleep(0.2)

if __name__ == '__main__':
    work_process = multiprocessing.Process(target=work)
    work_process.daemon=True
    work_process.start()

    # 程序等待1秒
    time.sleep(1)
    print("程序结束") 

2.多线程

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-893o4ZDJ-1600777354156)(https://secure-static.wolai.com/static/2n35Qyv23wcQ9J14qHYo1L/image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6xMyrd1b-1600777354158)(https://secure-static.wolai.com/static/cWzBbmTqVQcLLrn6sLbY4B/image.png)]

线程之间的运行是无顺序的,取决于CPU的调度

import time
import threading

def task():
    time.sleep(1)
    # current_thread 获取当前的线程对象
    thread = threading.current_thread
    print('n')
    print(threading)

if __name__ == "__main__":
    for i in range(5):
        sub_thread = threading.Thread(target=task)
        sub_thread.start()

3.进程和线程的关系

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bXXzRmT1-1600777354160)(https://secure-static.wolai.com/static/9SEAYGTQDKXBRgYW5KSURp/image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HJOoVsaM-1600777354162)(https://secure-static.wolai.com/static/2VVG9UszCXxJXk9oM2ZcQ7/image.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-M8BWp5dP-1600777354163)(https://secure-static.wolai.com/static/vivG8fpSkLhuJ7mpaTfngf/image.png)]

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页