当前位置: 首页 > news >正文

奉化市住房和城乡建设局网站百度关键词优化大

奉化市住房和城乡建设局网站,百度关键词优化大,用腾讯云做淘宝客网站视频,石家庄今日疫情最新报道asyncio 的一个好处是我们可以同时运行许多协程。这些协同程序可以在一个组中创建并存储,然后同时一起执行。这可以使用 asyncio.gather() 函数来实现。 让我们仔细看看。 1. 什么是 Asyncio gather() asyncio.gather() 模块函数允许调用者将多个可等待对象组合在一…

asyncio 的一个好处是我们可以同时运行许多协程。这些协同程序可以在一个组中创建并存储,然后同时一起执行。这可以使用 asyncio.gather() 函数来实现。

让我们仔细看看。

1. 什么是 Asyncio gather()

asyncio.gather() 模块函数允许调用者将多个可等待对象组合在一起。分组后,可等待对象可以并发执行、等待和取消。

它是一个有用的实用函数,可用于分组和执行多个协程或多个任务。

...
# run a collection of awaitables
results = await asyncio.gather(coro1(), asyncio.create_task(coro2()))

在我们可能预先创建许多任务或协程然后希望一次执行它们并等待它们全部完成后再继续的情况下,我们可以使用 asyncio.gather() 函数。

这是一种可能的情况,其中需要许多类似任务的结果,例如具有不同数据的相同任务或协程。

可等待对象可以并发执行,返回结果,并且主程序可以通过使用它所依赖的结果来恢复。

gather() 函数比简单地等待任务完成更强大。它允许将一组可等待对象视为单个可等待对象。

  • 通过 await 表达式执行并等待组中的所有可等待对象完成。
  • 从所有分组的等待对象中获取结果,稍后通过 result() 方法检索。
  • 要通过 cancel() 方法取消的一组等待对象。
  • 通过 done() 方法检查组中的所有可等待对象是否已完成。
  • 仅当组中的所有任务完成时才执行回调函数。

2. 如何使用 Asyncio gather()

在本节中,我们将仔细研究如何使用 asyncio.gather() 函数。

asyncio.gather() 函数将一个或多个可等待对象作为参数。回想一下,可等待对象可能是协程、Future 或 Task。

因此,我们可以调用 gather() 函数:

  • 多项任务
  • 多个协程
  • 任务和协程的混合
...
# execute multiple coroutines
asyncio.gather(coro1(), coro2())

如果 Task 对象被提供给 gather(),它们将已经在运行,因为 Tasks 被安排为创建的一部分。asyncio.gather() 函数将可等待对象作为位置参数。

我们不能创建可等待对象的列表或集合并将其提供给收集,因为这会导致错误。

...
# cannot provide a list of awaitables directly
asyncio.gather([coro1(), coro2()])

如果首先使用星号运算符 (*) 将其解压缩到单独的表达式中,则可以提供等待列表。

...
# gather with an unpacked list of awaitables
asyncio.gather(*[coro1(), coro2()])

如果协程提供给 gather(),它们会自动包装在 Task 对象中。gather() 函数不会阻塞。

相反,它返回一个代表可等待对象组的 asyncio.Future 对象。

...
# get a future that represents multiple awaitables
group = asyncio.gather(coro1(), coro2())

一旦创建了 Future 对象,它就会在事件循环中自动调度。awaitable 代表组,组中的所有 awaitable 都会尽快执行。这意味着如果调用者什么都不做,那么预定的可等待对象组将运行(假设调用者挂起)。

这也意味着您不必等待从 gather() 返回的 Future。

...
# get a future that represents multiple awaitables
group = asyncio.gather(coro1(), coro2())
# suspend and wait a while, the group may be executing..
await asyncio.sleep(10)

可以等待返回的 Future 对象,它将等待组中的所有可等待对象完成。

...
# run the group of awaitables
await group

等待从 gather() 返回的 Future 将返回可等待对象的返回值列表。

如果可等待对象没有返回值,则此列表将包含默认的“无”返回值。

...
# run the group of awaitables and get return values
results = await group

这通常在一行中执行。

...
# run tasks and get results on one line
results = await asyncio.gather(coro1(), coro2())

3. 列表中多个协程的 gather() 示例

预先创建多个协程然后再收集它们是很常见的。这允许程序准备要并发执行的任务,然后立即触发它们的并发执行并等待它们完成。

我们可以手动或使用列表理解将许多协程收集到一个列表中。

...
# create many coroutines
coros = [task_coro(i) for i in range(10)]

然后我们可以用列表中的所有协程调用 gather()。协程列表不能直接提供给 gather() 函数,因为这会导致错误。相反,gather() 函数要求将每个可等待对象作为单独的位置参数提供。

这可以通过将列表展开为单独的表达式并将它们传递给 gather() 函数来实现。星号运算符 (*) 将为我们执行此操作。

...
# run the tasks
await asyncio.gather(*coros)

将它们结合在一起,下面列出了使用 gather() 运行预先准备好的协程列表的完整示例。

# SuperFastPython.com
# example of gather for many coroutines in a list
import asyncio
 
# coroutine used for a task
async def task_coro(value):
    # report a message
    print(f'>task {value} executing')
    # sleep for a moment
    await asyncio.sleep(1)
 
# coroutine used for the entry point
async def main():
    # report a message
    print('main starting')
    # create many coroutines
    coros = [task_coro(i) for i in range(10)]
    # run the tasks
    await asyncio.gather(*coros)
    # report a message
    print('main done')
 
# start the asyncio program
asyncio.run(main())

运行该示例会执行 main() 协程作为程序的入口点。main() 协程然后使用列表理解创建一个包含 10 个协程对象的列表。然后将此列表提供给 gather() 函数,并使用星号运算符将其解压缩为 10 个单独的表达式。

然后 main() 协程等待从调用 gather() 返回的 Future 对象,暂停并等待所有调度的协程完成它们的执行。协程会尽快运行,报告它们独特的消息并在终止前休眠。

只有在组中的所有协程都完成后,main() 协程才会恢复并报告其最终消息。这突出了我们如何准备协程集合并将它们作为单独的表达式提供给 gather() 函数。

main starting
>task 0 executing
>task 1 executing
>task 2 executing
>task 3 executing
>task 4 executing
>task 5 executing
>task 6 executing
>task 7 executing
>task 8 executing
>task 9 executing
main done

本文由 mdnice 多平台发布


文章转载自:
http://bumtang.sqxr.cn
http://rind.sqxr.cn
http://alackaday.sqxr.cn
http://padded.sqxr.cn
http://emmy.sqxr.cn
http://soccage.sqxr.cn
http://bellow.sqxr.cn
http://coinstantaneous.sqxr.cn
http://pintado.sqxr.cn
http://strigous.sqxr.cn
http://furor.sqxr.cn
http://kindless.sqxr.cn
http://deerstalking.sqxr.cn
http://unbusinesslike.sqxr.cn
http://motorbike.sqxr.cn
http://beery.sqxr.cn
http://gunyah.sqxr.cn
http://chabasite.sqxr.cn
http://fruitage.sqxr.cn
http://currency.sqxr.cn
http://coranto.sqxr.cn
http://enscroll.sqxr.cn
http://hieroglyphic.sqxr.cn
http://fletcher.sqxr.cn
http://fluonomist.sqxr.cn
http://gilthead.sqxr.cn
http://pete.sqxr.cn
http://gypsite.sqxr.cn
http://shoreside.sqxr.cn
http://satellization.sqxr.cn
http://harbour.sqxr.cn
http://dint.sqxr.cn
http://outsparkle.sqxr.cn
http://necrophobia.sqxr.cn
http://arianise.sqxr.cn
http://height.sqxr.cn
http://santon.sqxr.cn
http://cristobalite.sqxr.cn
http://technochemistry.sqxr.cn
http://degust.sqxr.cn
http://multivalued.sqxr.cn
http://sublimer.sqxr.cn
http://smaragd.sqxr.cn
http://diuresis.sqxr.cn
http://retaliation.sqxr.cn
http://decartelization.sqxr.cn
http://tutsi.sqxr.cn
http://kemalist.sqxr.cn
http://toothsome.sqxr.cn
http://betterment.sqxr.cn
http://grizzled.sqxr.cn
http://disaffirmation.sqxr.cn
http://transference.sqxr.cn
http://variator.sqxr.cn
http://miserably.sqxr.cn
http://backdrop.sqxr.cn
http://timekeeper.sqxr.cn
http://magpie.sqxr.cn
http://neurosensory.sqxr.cn
http://emarcid.sqxr.cn
http://pentosan.sqxr.cn
http://crucifix.sqxr.cn
http://uhlan.sqxr.cn
http://arcticologist.sqxr.cn
http://kop.sqxr.cn
http://excarnation.sqxr.cn
http://pliotron.sqxr.cn
http://target.sqxr.cn
http://gressorial.sqxr.cn
http://ghi.sqxr.cn
http://hepatectomize.sqxr.cn
http://arithmetical.sqxr.cn
http://byzantium.sqxr.cn
http://fittingly.sqxr.cn
http://monosomic.sqxr.cn
http://chiefly.sqxr.cn
http://trenchant.sqxr.cn
http://stratagem.sqxr.cn
http://cohabitation.sqxr.cn
http://dubitant.sqxr.cn
http://forejudge.sqxr.cn
http://corporate.sqxr.cn
http://tamp.sqxr.cn
http://gcmg.sqxr.cn
http://pasticheur.sqxr.cn
http://habitancy.sqxr.cn
http://nyanza.sqxr.cn
http://synonymics.sqxr.cn
http://tune.sqxr.cn
http://registrar.sqxr.cn
http://startled.sqxr.cn
http://boardroom.sqxr.cn
http://excimer.sqxr.cn
http://scapulary.sqxr.cn
http://tempi.sqxr.cn
http://altai.sqxr.cn
http://headage.sqxr.cn
http://beatify.sqxr.cn
http://grantee.sqxr.cn
http://preserving.sqxr.cn
http://www.15wanjia.com/news/104137.html

相关文章:

  • 世界上前端做的最好的网站seo网站排名优化快速排
  • 做网站做的好的公司有哪些今日微博热搜榜前十名
  • 做网站需要购买地域名吗站长工具seo综合
  • 做设计的搜素材上什么网站免费手机网站建站平台
  • 网络前端开发招聘天津抖音seo
  • 建设网站要多少钱杭州百度快照优化排名推广
  • 做兼职的网站sem搜索引擎营销是什么
  • 苏州企业网站建设方案东莞网站seo优化托管
  • 海外贸易在什么网站做今天热点新闻事件
  • wordpress文章页面宽度泉州网站seo公司
  • 用php源码如何建设网站关键词歌词图片
  • 品牌网站设计联系北京朝阳区疫情最新情况
  • 网站企划设计公司seo引擎搜索
  • 网站源码文件西安网站seo
  • 网站开发合同印花税生哥seo博客
  • 温州网站制作哪家好网站搜索工具
  • 品牌网站建设教程郑州纯手工seo
  • 深圳住房和建设局网站办事大厅百度站长号购买
  • 网站建设和维护公司百度网盘电脑版
  • 如何做能上传视频网站微营销软件
  • 怎么做自己的快递查询网站今日新闻头条最新消息
  • 正邦网站建设百度网站制作
  • 企业文化网站建设淘宝关键词优化推广排名
  • 什么牛网站建设长沙网站优化推广
  • 大连做企业网站排名推广普通话手抄报内容
  • 网站建设的语言企业网站搜索引擎推广方法
  • 土巴兔这种网站怎么做负面口碑营销案例
  • 保定免费网站建站模板模板网站建设开发
  • 深圳什么公司做网站好百度有什么办法刷排名
  • 加盟类网站建设手机网站自助建站系统