进程+线程+协程

进程+线程+协程

  • [1 进程](#1 进程)
    • [1.1 无进程](#1.1 无进程)
    • [1.2 多进程](#1.2 多进程)
    • [1.3 p1.join()](#1.3 p1.join())
    • [1.4 权重](#1.4 权重)
    • [1.5 全局变量list](#1.5 全局变量list)

1 进程

1.1 无进程

不使用进程,task_01和task_02先后执行

python 复制代码
import os
import time
from  multiprocessing import Process

def task_01():
    print("*"*8+' task_01 '+"*"*8)
    i = 0
    while True:
        time.sleep(0.5)
        print('task_01:  %d'%i)
        i += 1
        if i>10:
            break

def task_02():
    print("*"*8+' task_02 '+"*"*8)
    i = 0
    while True:
        time.sleep(1)
        print('task_02:  %d'%i)
        i += 10
        if i>30:
            break


if __name__ == '__main__':
    print("*"*8+' 主程序开始 '+"*"*8)
    task_01()
    task_02()
    print("#" * 8 + ' 主程序结束 ' + "#" * 8)
'''
## 运行结果
******** 主程序开始 ********
******** task_01 ********
task_01:  0
task_01:  1
task_01:  2
task_01:  3
task_01:  4
task_01:  5
task_01:  6
task_01:  7
task_01:  8
task_01:  9
task_01:  10
******** task_02 ********
task_02:  0
task_02:  10
task_02:  20
task_02:  30
######## 主程序结束 ########
Process finished with exit code 0
'''

1.2 多进程

父程序和子程序各自执行,互不干扰。由于只有一个核,task_01和task_02交替执行。

python 复制代码
import os
import time
from  multiprocessing import Process

def task_01():
    print("*"*8+' task_01 '+"*"*8)
    i = 0
    while True:
        time.sleep(0.5)
        print('task_01:  %d'%i)
        i += 1
        if i>10:
            break

def task_02():
    print("*"*8+' task_02 '+"*"*8)
    i = 0
    while True:
        time.sleep(1)
        print('task_02:  %d'%i)
        i += 10
        if i>100:
            break


if __name__ == '__main__':
    print("*"*8+' 主程序开始 '+"*"*8)
    p1 = Process(target = task_01,name="任务1")  # 子程序实例化
    p2 = Process(target = task_02,name="任务2")
    p1.start()   # 开启子程序
    p2.start()
    print("#" * 8 + ' 主程序结束 ' + "#" * 8)

## 运行结果
'''
******** 主程序开始 ********
######## 主程序结束 ########
******** task_01 ********
******** task_02 ********
task_01:  0
task_02:  0
task_01:  1
task_01:  2
task_02:  10
task_01:  3
task_01:  4
task_02:  20
task_01:  5
task_01:  6
task_02:  30
task_01:  7
task_01:  8
task_02:  40
task_01:  9
task_01:  10
task_02:  50
task_02:  60
task_02:  70
task_02:  80
task_02:  90
task_02:  100

Process finished with exit code 0
'''

1.3 p1.join()

p1.join() 表示主进程要等子进程执行完再执行p1.join()后面的代码

python 复制代码
# 
import os
import time
from  multiprocessing import Process

def task_01(name,s):
    print("*"*8+' task_01 '+"*"*8)
    i = 0
    while True:
        time.sleep(s)
        print(' %s:  task_01.id: %d, i:  %d'%(name,os.getpid(),i))
        i += 1
        if i>40:
            break

def task_02(name,s):
    print("*"*8+' task_02 '+"*"*8)
    j = 0
    while True:
        time.sleep(s)
        print(' %s:  task_02.id: %d, j:  %d'%(name,os.getpid(),j))
        j += 10
        if j>300:
            break


if __name__ == '__main__':
    print("*"*8+' 主程序开始 '+"*"*8)
    p1 = Process(target = task_01,name="任务1",args=("A",0.5))  #  给task_01传递参数
    p2 = Process(target = task_02,name="任务2",args=("B",1))
    count = 0
    p1.start()   # 开启子程序
    p2.start()
    print('p1.name: ',p1.name)
    print('p2.name: ',p2.name)


    ## 通过主程序控制子程序
    count = 0
    while True:
        time.sleep(0.3)
        count += 5
        print('count: ', count)
        if count== 50:
            p1.terminate()
            p2.terminate()
            break
    print("#" * 8 + ' 主程序结束 ' + "#" * 8)
'''
******** 主程序开始 ********
p1.name:  任务1
p2.name:  任务2
******** task_01 ********
******** task_02 ********
count:  5
count:  10
 A:  task_01.id: 3603, i:  0
count:  15
 A:  task_01.id: 3603, i:  1
 B:  task_02.id: 3604, j:  0
count:  20
count:  25
 A:  task_01.id: 3603, i:  2
count:  30
count:  35
 B:  task_02.id: 3604, j:  10
 A:  task_01.id: 3603, i:  3
count:  40
 A:  task_01.id: 3603, i:  4
count:  45
count:  50
######## 主程序结束 ########

Process finished with exit code 0
'''

1.4 权重

进程之间不共享权重

python 复制代码
import os
import time
from  multiprocessing import Process
N = 1
def task_01(name,s):
    print("*"*8+' task_01 '+"*"*8)
    global N
    while True:
        time.sleep(s)
        print(' %s:  task_01.id: %d, N:  %d'%(name,os.getpid(),N))
        N += 1
        if N>20:
            break

def task_02(name,s):
    print("*"*8+' task_02 '+"*"*8)
    global N
    while True:
        time.sleep(s)
        print(' %s:  task_02.id: %d, N:  %d'%(name,os.getpid(),N))
        N += 100
        if N>1000:
            break


if __name__ == '__main__':
    print("*"*8+' 主程序开始 '+"*"*8)
    p1 = Process(target = task_01,name="任务1",args=("A",0.5))  #  给task_01传递参数
    p2 = Process(target = task_02,name="任务2",args=("B",1))
    N = 100
    p1.start()   # 开启子程序
    p2.start()
    print('p1.name: ',p1.name)
    print('p2.name: ',p2.name)



    print("#" * 8 + ' 主程序结束 ' + "#" * 8)
'''    
******** 主程序开始 ********
p1.name:  任务1
p2.name:  任务2
######## 主程序结束 ########
******** task_02 ********
******** task_01 ********
 A:  task_01.id: 3710, N:  1
 B:  task_02.id: 3711, N:  1
 A:  task_01.id: 3710, N:  2
 A:  task_01.id: 3710, N:  3
 B:  task_02.id: 3711, N:  101
 A:  task_01.id: 3710, N:  4
 A:  task_01.id: 3710, N:  5
 B:  task_02.id: 3711, N:  201
 A:  task_01.id: 3710, N:  6
 A:  task_01.id: 3710, N:  7
 B:  task_02.id: 3711, N:  301
 A:  task_01.id: 3710, N:  8
 A:  task_01.id: 3710, N:  9
 B:  task_02.id: 3711, N:  401
 A:  task_01.id: 3710, N:  10
 A:  task_01.id: 3710, N:  11
 B:  task_02.id: 3711, N:  501
 A:  task_01.id: 3710, N:  12
 A:  task_01.id: 3710, N:  13
 B:  task_02.id: 3711, N:  601
 A:  task_01.id: 3710, N:  14
 A:  task_01.id: 3710, N:  15
 B:  task_02.id: 3711, N:  701
 A:  task_01.id: 3710, N:  16
 A:  task_01.id: 3710, N:  17
 B:  task_02.id: 3711, N:  801
 A:  task_01.id: 3710, N:  18
 A:  task_01.id: 3710, N:  19
 B:  task_02.id: 3711, N:  901
 A:  task_01.id: 3710, N:  20

Process finished with exit code 0
'''

1.5 全局变量list

进程之间不共享全局变量list

python 复制代码
# 
import os
import time
import random
from  multiprocessing import Process
N = []
def task_01(name,s):
    print("*"*8+' task_01 '+"*"*8)
    global N
    while True:
        time.sleep(s)
        print(' {}:  task_01.id: {}, N1:  {}'.format(name,os.getpid(),N))
        N.append(-round(random.random(),1))
        if len(N)>9:
            break

def task_02(name,s):
    print("*"*8+' task_02 '+"*"*8)
    global N
    while True:
        time.sleep(s)
        print(' {}:  task_02.id: {}, N2:  {}'.format(name,os.getpid(),N))
        N.append(round(random.random())*100)
        if len(N)>8:
            break


if __name__ == '__main__':
    print("*"*8+' 主程序开始 '+"*"*8)
    p1 = Process(target = task_01,name="任务1",args=("A",0.5))  #  给task_01传递参数
    p2 = Process(target = task_02,name="任务2",args=("B",1))
    N = 100
    p1.start()   # 开启子程序
    p2.start()
    print('p1.name: ',p1.name)
    print('p2.name: ',p2.name)



    print("#" * 8 + ' 主程序结束 ' + "#" * 8)
    time.sleep(10)
    print('NNNNNN: ', N)
'''
******** 主程序开始 ********
p1.name:  任务1
p2.name:  任务2
######## 主程序结束 ########
******** task_01 **************** task_02 ********

 A:  task_01.id: 3819, N1:  []
 B:  task_02.id: 3820, N2:  []
 A:  task_01.id: 3819, N1:  [-0.8]
 A:  task_01.id: 3819, N1:  [-0.8, -0.7]
 A:  task_01.id: 3819, N1:  [-0.8, -0.7, -0.8] 
 B:  task_02.id: 3820, N2:  [0]

 A:  task_01.id: 3819, N1:  [-0.8, -0.7, -0.8, -0.3]
 B:  task_02.id: 3820, N2:  [0, 0]
 A:  task_01.id: 3819, N1:  [-0.8, -0.7, -0.8, -0.3, -0.3]
 A:  task_01.id: 3819, N1:  [-0.8, -0.7, -0.8, -0.3, -0.3, -0.5]
 B:  task_02.id: 3820, N2:  [0, 0, 100]
 A:  task_01.id: 3819, N1:  [-0.8, -0.7, -0.8, -0.3, -0.3, -0.5, -0.3]
 A:  task_01.id: 3819, N1:  [-0.8, -0.7, -0.8, -0.3, -0.3, -0.5, -0.3, -0.9]
 B:  task_02.id: 3820, N2:  [0, 0, 100, 100]
 A:  task_01.id: 3819, N1:  [-0.8, -0.7, -0.8, -0.3, -0.3, -0.5, -0.3, -0.9, -0.2]
 B:  task_02.id: 3820, N2:  [0, 0, 100, 100, 100]
 B:  task_02.id: 3820, N2:  [0, 0, 100, 100, 100, 100]
 B:  task_02.id: 3820, N2:  [0, 0, 100, 100, 100, 100, 0]
 B:  task_02.id: 3820, N2:  [0, 0, 100, 100, 100, 100, 0, 0]
NNNNNN:  100

Process finished with exit code 0
'''
相关推荐
B站_计算机毕业设计之家24 分钟前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法
渣渣苏32 分钟前
Langchain实战快速入门
人工智能·python·langchain
lili-felicity41 分钟前
CANN模型量化详解:从FP32到INT8的精度与性能平衡
人工智能·python
数据知道44 分钟前
PostgreSQL实战:详解如何用Python优雅地从PG中存取处理JSON
python·postgresql·json
ZH15455891311 小时前
Flutter for OpenHarmony Python学习助手实战:面向对象编程实战的实现
python·学习·flutter
玄同7651 小时前
SQLite + LLM:大模型应用落地的轻量级数据存储方案
jvm·数据库·人工智能·python·语言模型·sqlite·知识图谱
User_芊芊君子1 小时前
CANN010:PyASC Python编程接口—简化AI算子开发的Python框架
开发语言·人工智能·python
白日做梦Q1 小时前
Anchor-free检测器全解析:CenterNet vs FCOS
python·深度学习·神经网络·目标检测·机器学习
喵手1 小时前
Python爬虫实战:公共自行车站点智能采集系统 - 从零构建生产级爬虫的完整实战(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集公共自行车站点·公共自行车站点智能采集系统·采集公共自行车站点导出csv
喵手2 小时前
Python爬虫实战:地图 POI + 行政区反查实战 - 商圈热力数据准备完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·地区poi·行政区反查·商圈热力数据采集