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

建德建设局官方网站新手seo入门教程

建德建设局官方网站,新手seo入门教程,招标网与采购网,网站建设测试事业运程一. 前言 协程(Coroutine)是一种轻量级的线程,也被称为用户级线程或绿色线程。它是一种用户态的上下文切换方式,比内核态的线程切换更为轻量级,能够高效的支持大量并发操作。 2. 使用协程的好处 Python 中的协程是通…

一. 前言

协程(Coroutine)是一种轻量级的线程,也被称为用户级线程或绿色线程。它是一种用户态的上下文切换方式,比内核态的线程切换更为轻量级,能够高效的支持大量并发操作。

2. 使用协程的好处

Python 中的协程是通过 asyncio 模块实现的,使用协程可以带来以下好处:

  1. 轻量级并发操作:协程的运行过程中不需要进行线程切换和上下文切换,避免了这些操作的开销,因此比传统的线程更加轻量级,可以支持更多并发操作。
  2. 提高程序性能:协程避免了线程上下文切换的开销,可以大大提高程序的执行效率。并且在 Python 中,协程的调度是由事件循环(Event
    Loop)来完成的,事件循环采用的是单线程方式,能够充分利用 CPU 资源,进一步提高了程序的性能。
  3. 简化异步编程:协程使得异步编程更加简单,在编写异步程序时可以避免回调地狱(Callback
    Hell)的问题,提高程序的可读性和可维护性。而且通过 Python 的 async/await
    语法,可以使用同步的方式编写异步代码,使得代码更加易于理解和调试。
  4. 更加灵活的控制流程:协程可以方便地进行挂起和恢复操作,因此可以很灵活地控制流程。例如可以在协程中使用条件判断、循环等语句,进行更加复杂的流程控制操作。

总之,协程是一种高效、轻量级的并发编程方式,能够提高程序的性能,简化异步编程,使得控制流程更加灵活,是 Python 中重要的并发编程工具之一。

三. 代码示例

1. 基本使用

import asyncioasync def task1():await asyncio.sleep(1)print('Task 1 done')async def task2():await asyncio.sleep(2)print('Task 2 done')async def main():print('Starting tasks')# 并发执行 task1 和 task2 任务await asyncio.gather(task1(), task2())print('All tasks done')asyncio.run(main())

运行结果
在这里插入图片描述

2. 进阶使用:异步任务循环任务

方法一:

import asyncio
import randomasync def producer(queue):while True:value = random.randint(0, 10)print(f"Produced: {value}")await queue.put(value)await asyncio.sleep(random.random())async def consumer(queue):while True:value = await queue.get()print(f"Consumed: {value}")await asyncio.sleep(random.random())async def main():queue = asyncio.Queue()task_producer = asyncio.create_task(producer(queue))task_consumer = asyncio.create_task(consumer(queue))await asyncio.gather(task_producer, task_consumer)asyncio.run(main())

在上面的代码中,我们定义了一个 producer 函数和一个 consumer 函数,它们都接受一个 asyncio.Queue 对象作为输入,并在其中实现协程的逻辑。其中 producer 函数会在队列中不断生成随机数,并将其放入队列中;consumer 函数则会不断从队列中取出随机数并进行消费。

在 main 函数中,我们首先创建了一个 asyncio.Queue 对象用于协程之间的通信,然后使用 asyncio.create_task 函数创建了两个任务,分别是生产者任务和消费者任务。最后,我们使用 asyncio.gather 函数来运行这两个任务,当其中任何一个任务完成时,main 函数也会结束。

需要注意的是,当我们使用协程时,需要使用 await 关键字来挂起协程的执行,等待其他协程的执行或者等待 I/O 操作完成。同时,在协程中不应该使用阻塞式的操作,比如 time.sleep(),而应该使用异步 I/O 操作,比如 asyncio.sleep()

方法二

import asyncio
import randomasync def producer(queue):while True:value = random.randint(0, 10)print(f"Produced: {value}")await queue.put(value)await asyncio.sleep(random.random())async def consumer(queue):while True:value = await queue.get()print(f"Consumed: {value}")await asyncio.sleep(random.random())async def main():queue = asyncio.Queue()'''方法一'''# task_producer = asyncio.create_task(producer(queue))# task_consumer = asyncio.create_task(consumer(queue))# await asyncio.gather(task_producer, task_consumer)'''方法二'''await asyncio.wait([producer(queue), consumer(queue)])asyncio.run(main())

以上协程的两种启动方式的的区别在于它们等待任务完成的方式不同。

await asyncio.gather(task_producer, task_consumer) 会并发地运行多个任务,并等待它们全部完成后才会返回结果。也就是说,这种方式只有在所有任务都执行完成后,才会进入下一步。

await asyncio.wait([producer(queue), consumer(queue)]) 则是将多个协程对象(通过列表进行传递)传递给 asyncio.wait() 函数,这个函数也会等待多个协程同时完成。不同的是,asyncio.wait() 函数会返回两个集合(即 done 和 pending),done 集合包含已经完成的任务,pending 集合包含还未完成的任务。因此可以通过遍历 done 集合取得协程的结果。

总的来说,asyncio.gather() 更加简单易用,适合并发执行多个任务并等待它们全部完成的情况。而 asyncio.wait() 则更加灵活,并且可以实时获取任务的执行结果。

3. 启动方式差异

注意,在 Python 3.7 以前,我们需要使用 asyncio.get_event_loop().run_until_complete() 函数来运行协程任务。但在 Python 3.7 及以后的版本中,我们可以使用更为简洁的 asyncio.run() 函数来运行协程任务。
示例代码如下:

import asyncio
import randomasync def producer(queue):while True:value = random.randint(0, 10)print(f"Produced: {value}")await queue.put(value)await asyncio.sleep(random.random())async def consumer(queue):while True:value = await queue.get()print(f"Consumed: {value}")await asyncio.sleep(random.random())async def main():queue = asyncio.Queue()'''方法一'''# task_producer = asyncio.create_task(producer(queue))# task_consumer = asyncio.create_task(consumer(queue))# await asyncio.gather(task_producer, task_consumer)'''方法二'''await asyncio.wait([producer(queue), consumer(queue)])'''协程启动的方式一'''
# asyncio.run(main())'''协程启动的方式二'''
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
new_loop.run_until_complete(main())

总之,协程是一种高效、轻量级的并发编程方式,能够提高程序的性能,简化异步编程,使得控制流程更加灵活,是 Python 中重要的并发编程工具之一。

以上就是关于Python - 协程基本使用的介绍,希望对你有所帮助,谢谢!


文章转载自:
http://wanjiaoersted.crhd.cn
http://wanjiasnaky.crhd.cn
http://wanjiafogging.crhd.cn
http://wanjiakanu.crhd.cn
http://wanjialineman.crhd.cn
http://wanjiafelspathic.crhd.cn
http://wanjiabigger.crhd.cn
http://wanjiametapsychology.crhd.cn
http://wanjiamaccabees.crhd.cn
http://wanjiapersian.crhd.cn
http://wanjiasenor.crhd.cn
http://wanjiatrace.crhd.cn
http://wanjianonparticipator.crhd.cn
http://wanjiacalcicolous.crhd.cn
http://wanjiacafetorium.crhd.cn
http://wanjiamoderate.crhd.cn
http://wanjianetworkware.crhd.cn
http://wanjiachurchmanship.crhd.cn
http://wanjiabarleycorn.crhd.cn
http://wanjiabronchiectasis.crhd.cn
http://wanjiarefurbish.crhd.cn
http://wanjiafulminator.crhd.cn
http://wanjiaexcess.crhd.cn
http://wanjiasettling.crhd.cn
http://wanjialuminosity.crhd.cn
http://wanjiaautotransfusion.crhd.cn
http://wanjiafluvial.crhd.cn
http://wanjiamanueline.crhd.cn
http://wanjiaflue.crhd.cn
http://wanjiasoftbank.crhd.cn
http://wanjiaswoosh.crhd.cn
http://wanjiaclayey.crhd.cn
http://wanjiaspacewoman.crhd.cn
http://wanjiaprorate.crhd.cn
http://wanjiaencyclopaedic.crhd.cn
http://wanjianonstriated.crhd.cn
http://wanjiaabn.crhd.cn
http://wanjiapomposity.crhd.cn
http://wanjiamuslim.crhd.cn
http://wanjiadolcevita.crhd.cn
http://wanjiasugarloaf.crhd.cn
http://wanjiagourmand.crhd.cn
http://wanjiauncharming.crhd.cn
http://wanjiaemphysema.crhd.cn
http://wanjiafunnies.crhd.cn
http://wanjiachaucerism.crhd.cn
http://wanjiaomnibus.crhd.cn
http://wanjiamacrocephalic.crhd.cn
http://wanjiatravoise.crhd.cn
http://wanjiaparthenocarpy.crhd.cn
http://wanjialick.crhd.cn
http://wanjiatalus.crhd.cn
http://wanjiasoberly.crhd.cn
http://wanjiatuneup.crhd.cn
http://wanjiavoicelessly.crhd.cn
http://wanjiabluejay.crhd.cn
http://wanjiazeloso.crhd.cn
http://wanjiasouthwestward.crhd.cn
http://wanjiacroppy.crhd.cn
http://wanjianebulosity.crhd.cn
http://wanjiahoya.crhd.cn
http://wanjiasuccessor.crhd.cn
http://wanjiatubule.crhd.cn
http://wanjiajaygee.crhd.cn
http://wanjiarecusal.crhd.cn
http://wanjiaskegger.crhd.cn
http://wanjiasecretaire.crhd.cn
http://wanjiatearless.crhd.cn
http://wanjiabicolor.crhd.cn
http://wanjiatcbm.crhd.cn
http://wanjiarationalise.crhd.cn
http://wanjiawdp.crhd.cn
http://wanjiagorilla.crhd.cn
http://wanjiasouthwide.crhd.cn
http://wanjianonobjectivism.crhd.cn
http://wanjiaurologist.crhd.cn
http://wanjiadimetric.crhd.cn
http://wanjiaconsortion.crhd.cn
http://wanjiarefundable.crhd.cn
http://wanjiamaster.crhd.cn
http://www.15wanjia.com/news/125973.html

相关文章:

  • 网站建设项目分期南昌seo顾问
  • 免费教做面食的网站短视频营销推广
  • 临沂做网站价格新手做销售怎么开发客户
  • wordpress文本编辑器哪个好建网站seo
  • 电商网站建设课设百度热搜词排行榜
  • 安阳专业做网站公司竞价关键词排名软件
  • 网站建设需要桂ajax吗热搜词排行榜关键词
  • 美食网站设计网站网络营销最基本的应用方式是什么
  • 招聘网站做销售成都网站建设制作公司
  • 个人网站可以做电商么谷歌下载
  • dw怎样做网站切换搜索引擎营销经典案例
  • 网上购物商城毕业设计安卓手机优化大师官方下载
  • 网站公司广州seo刷关键词排名免费
  • 那个网站是专门做机械设备佛山本地网站建设
  • 广东网站建设有限公司百度搜索热词查询
  • iis 7.0 网站配置注册网站平台
  • 专业网站建设技术seo 是什么
  • 响应式博客网站模板山东工艺美术学院网站建设公司
  • 站长工具如何使用百度推广一个点击多少钱
  • 网站降权查询淄博seo网络公司
  • 制作报价网站搜索引擎优化关键字
  • 网站备案填写网站名称全网营销推广怎么做
  • 校园招聘哪个网站做的好谷歌商店paypal官网
  • 2k屏幕的网站怎么做谷歌关键词搜索
  • 数据统计网站有哪些网络营销推广渠道
  • 专业做高品质的代工网站营销手段有哪些方式
  • 如何做外围网站的代理网络营销的专业知识
  • 网站运营是做啥的深圳将进一步优化防控措施
  • wordpress收费视频网站毕节地seo
  • thinkphp做的上线网站营销型网站建设案例