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())
相关推荐
吃饱了得干活13 小时前
Spring Cloud Gateway 微服务网关:路由、断言、过滤器
java·spring cloud
Warson_L13 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅13 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅13 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L14 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅14 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
lwx5728014 小时前
探秘InnoDB:搞懂它的内存、线程、磁盘与日志刷盘策略
java·后端
Warson_L15 小时前
python的类&继承
python
Warson_L15 小时前
类型标注/type annotation
python
Flynt16 小时前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端