首页 > 其他分享 >asyncio 异步io相关知识

asyncio 异步io相关知识

时间:2024-04-03 21:25:13浏览次数:12  
标签:异步 start io time test async hello asyncio

import asyncio
#运行器
async def hello_async(delay, words):
    await asyncio.sleep(delay)
    print(words)
#协程与任务
async def test_async(): #等待第一个协程hello_async执行完以后再串行执行下一个任务,两个协程没有并发效果
    start_time = time.time()
    print('fun test_async run start_time', start_time)
    await hello_async(1, 'hello asyncio')
    await hello_async(2, 'hello asyncio')
    end_time = time.time()
    print('fun test_async run end_time %s, running time %s' % (end_time, end_time-start_time))

async def test_async1(): #asyncio.create_task实现两个异步任务并发
    start_time = time.time()
    print('fun test_async1 run start_time', start_time)
    task1 = asyncio.create_task(hello_async(1, 'hello asyncio'))
    task2 = asyncio.create_task(hello_async(2, 'hello asyncio'))
    await task1
    await task2
    end_time = time.time()
    print('fun test_async1 run end_time %s, running time %s' % (end_time, end_time-start_time))

async def test_async_tg(): #asyncio.TaskGroup()比直接使用create_task()方法更加方便,创建task一步任务后不用再await调用
    start_time = time.time()
    print('fun test_async_tg run start_time', start_time)
    async with asyncio.TaskGroup() as tg:
        task1 = tg.create_task(hello_async(1, 'hello asyncio'))
        task2 = tg.create_task(hello_async(2, 'hello asyncio'))
    end_time = time.time()
    print('fun test_async_tg run end_time %s, running time %s' % (end_time, end_time-start_time))

if __name__ == '__main__':
    asyncio.run(hello_async(1, 'hello asyncio'))
    # asyncip运行器上下文管理执行hello_async()
    with asyncio.Runner() as runner:
        runner.run(hello_async(1, 'hello asyncio'))
        runner.run(test_async())
        runner.run(test_async1())
        runner.run(test_async_tg())

 

标签:异步,start,io,time,test,async,hello,asyncio
From: https://www.cnblogs.com/flags-blog/p/18113516

相关文章

  • VMware Workstation Pro各版本下载链接汇总(特全!!!)
    VMwareWorkstationPro各版本下载链接汇总(10、11、12、14、15、16官网全版本)整理不易,点赞关注一下吧工具软件:VMwareWorkstationPro1.系统要求VM17:硬件要求较高,Windows10或更高版64位。VM16:硬件要求较高,Windows10或更高版64位。VM15:硬件要求中等,Windows7或更高......
  • ERROR: No matching distribution found for pymcubes
    (pytorch3drecgan)ubuntu@ubuntu:~/lcx/3D-RecGAN-pytorch-masterv3$pipinstallpymcubesWARNING:Keyringisskippedduetoanexception:Failedtounlockthecollection!WARNING:Retrying(Retry(total=4,connect=None,read=None,redirect=None,status=None))......
  • AI绘画:使用Stable Diffusion ComfyUI进行换脸:IPAdapter FaceID全面教程
        在数字艺术和媒体编辑领域,换脸技术已经成为一种流行且强大的工具。它允许创作者将一个人物的面部特征无缝地转移到另一个人物上,创造出令人信服的视觉作品。StableDiffusionComfyUI提供了一个高效的平台,让用户能够轻松地实现换脸。本文将详细介绍如何使用ComfyUI......
  • Nginx 配置反向代理时出现 502 Bad Gateway (13_ Permission denied) while connectin
    我们在Nginx配置反向代理后,可能会出现如下报错:502BadGateway(13:Permissiondenied)whileconnectingtoupstreamSorry,thepageyouarelookingforiscurrentlyunavailable.<br/>Pleasetryagainlater.这些错误,一般是由SELinux引起的(https://stackoverf......
  • 首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
    CodeGeeX是一款免费的智能编程助手。继CodeGeeX在VisualStudioCode、JetBrainsIDEs全家桶、HBuilderX、deepin-IDE等主流IDE中上线后,用户呼声最高的VisualStudio平台的适配插件产品也正式推出上线了!成为首个适配VisualStudio平台的国产智能编程助手。目前CodeGeeXv1.0.0适......
  • 使用 Validation
    使用Validation要使用验证,请使用class-validator。示例如何在TypeORM中使用class-validator:import{Entity,PrimaryGeneratedColumn,Column}from"typeorm";import{Contains,IsInt,Length,IsEmail,IsFQDN,IsDate,Min,Max}from"class-validator";......
  • [论文阅读] Domain generalization by learning and removing domain-specific featur
    1Introduction最近的研究发现,DNNs倾向于以与人类不同的方式学习决策规则[17,21,16]。例如,在基于ImageNet的图像分类任务中,卷积神经网络(CNNs)倾向于学习局部纹理以区分对象,而我们人类则可能使用全局对象形状的知识作为线索。DNNs学到的特征可能只属于特定的领域,对其他领域不具......
  • Transformer模型-Positional Encoding位置编码的简明介绍
    今天介绍transformer模型的positionalencoding 位置编码背景位置编码用于为序列中的每个标记或单词提供一个相对位置。在阅读句子时,每个单词都依赖于其周围的单词。例如,有些单词在不同的上下文中具有不同的含义,因此模型应该能够理解这些变化以及每个单词所依赖的上下文。......
  • 在Express中使用Cookie和Session
    在Express中使用Cookie和Session 如果想要通过NodeJS使用cookie,那么你需要安装"cookie-parser"包,如果是session那么需要安装"cookie-session"安装方法:npminstallcookie-parser-Snpminstallcookie-session-S然后在服务端引用varcookieParser=require('cookie-p......
  • HTTP响应头Content-Disposition
    Content-Disposition用于表示已什么方式展示文件,其中attachment是已附件的方式保存到文件夹当中,可以通过filename去指定附件的名称。但是filename仅支持ASSCII标准中的字符集,如果附件名是中文的话需要使用filename*并同时指定文件的编码:filename*=UTF-8''同时,如果设置Content-......