python的进程,线程、协程

python进程的实现

复制代码
#coding:utf-8
from multiprocessing import Process
import time


def run(name):
    print('%s is running' % name)
    time.sleep(3)
    print('%s finished his run' % name)


if __name__ == '__main__':
    p = Process(target=run, args=('XWenXiang',))  # 创建一个进程对象
    p.start()  # 告诉操作系统创建一个新的进程
    print('父进程')

python线程的实现

python协程实现的代码

(协程是在线程的基础上进一步切换)

复制代码
#coding:utf-8
import asyncio

async def hello():
    print("Hello")
    await asyncio.sleep(3)  # 模拟耗时操作
    print("World")

asyncio.run(hello())
相关推荐
aqi005 分钟前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
行者全栈架构师1 小时前
IDEA 中 Maven 项目的 15 个红色报错快速解决方法
java·后端
令人头秃的代码0_01 小时前
mac(m5)平台编译openjdk
java
金銀銅鐵2 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf3 小时前
Agent 流程编排
后端·python·agent
copyer_xyf3 小时前
Agent RAG
后端·python·agent
copyer_xyf3 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf3 小时前
Agent 记忆管理
后端·python·agent
星云穿梭19 小时前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵19 小时前
用 Pygame 实现 15 puzzle
python·数学·游戏