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())
相关推荐
大貔貅喝啤酒17 分钟前
Python Requests库教程
自动化测试·python·requests库
copyer_xyf1 小时前
LangChain 调用 LLM
后端·python·agent
copyer_xyf1 小时前
Prompt 组织管理
后端·python·agent
asdfg12589631 小时前
JavaBean是什么?怎么理解?有什么用途?
java·开发语言
dsyyyyy11011 小时前
JavaScript变量
开发语言·javascript·ecmascript
shimly1234562 小时前
python3 uvicorn 是啥?
python
z落落2 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
CTA量化套保2 小时前
期货量化程序 time.sleep 卡死:天勤单线程与 deadline 替代
python·区块链
allway23 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_462446233 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash