iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python Tulip ( async
  • 880
分享到

Python Tulip ( async

PythonTulipasync 2023-01-31 03:01:54 880人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

18.5.2.1. Event loop functions 事件循环函数The following functions are convenient shortcuts to accessing the methods of the gl

18.5.2.1. Event loop functions 事件循环函数

The following functions are convenient shortcuts to accessing the methods of the global policy. Note that this provides access to the default policy, unless an alternative policy was set by calling set_event_loop_policy() earlier in the execution of the process.

下面的函数是使用全局方法的一组快捷方式。注意仅仅是提供一个访问默认规则,在没有通过调用set_event_loop_policy()指定其他规则时有效。


  • asyncio.get_event_loop()

    Equivalent to calling get_event_loop_policy().get_event_loop().

   get_event_loop_policy().get_event_loop() 的等价调用。



     get_event_loop_policy().set_event_loop(loop)的等价调用。


       get_event_loop_policy().new_event_loop()的等价调用。



18.5.2.2. Available event loops 可用的事件循环

asyncio currently provides two implementations of event loops: SelectorEventLoop and ProactorEventLoop.

asyncio目前提供两种事件循环的实现:SelectorEventLoop 和ProactorEventLoop


       会使用所有可用平台上最高效的选择器。


       On windows, only Sockets are supported (ex: pipes are not supported): see the MSDN documentation of select.

      在Windows,只有sockets模式被支持(并且管道不支持):见MSDN documentation of select。


Example to use a ProactorEventLoop on Windows:

在Windows上使用ProactorEventLoop 的示例:

import asyncio, os
if os.name == 'nt':
   loop = asyncio.ProactorEventLoop()
   asyncio.set_event_loop(loop)


18.5.2.3. Platform support 平台支持

The asyncio module has been designed to be portable, but each platform still has subtle differences and may not support all asyncio features.

asyncio 模块被设计成便于迁移的,但是每个平台仍然有一些细微的区别,导致不能支持所有的asyncio 特性:


18.5.2.3.1. Windows

Common limits of Windows event loops:

Windows事件循环通常的限制:

       create_unix_connection() and create_unix_server()不被支持:socket.AF_UNIX端口族是UNIX规范。


       add_signal_handler() and remove_signal_handler()不被支持。



      EventLoopPolicy.set_child_watcher() 不支持。ProactorEventLoop 支持子进程。只有一个观测子进程的实现,不需要进行配置。


SelectorEventLoop specific limits:  SelectorEventLoop标准限制

      SelectSelector只能用于socket,并且限制最多512个。

      add_reader() and add_writer()只能接受socket文件描述符。

      管道不支持(如:connect_read_pipe()connect_write_pipe())。

      Subprocesses不被支持(如:subprocess_exec()subprocess_shell())。

ProactorEventLoop specific limits:  ProactorEventLoop标准限制:

  • create_datagram_endpoint() (UDP) is not supported

      create_datagram_endpoint() (UDP)不被支持。

  • add_reader() and add_writer() are not supported

      add_reader() and add_writer() 不被支持。

The resolution of the monotonic clock on Windows is usually around 15.6 msec. The best resolution is 0.5 msec. The resolution depends on the hardware (availability of HPET) and on the Windows configuration. See asyncio delayed calls.

Windows上绝对时钟的解析度一般是15.6毫秒,最好的解析度为0.5毫秒。解析度依赖于硬件(HPET的可用性)以及Windows配置。见asyncio delayed calls

Changed in version 3.5: ProactorEventLoop now supports SSL.  3.5版中的改变:ProactorEventLoop已支持SSL。


18.5.2.3.2. Mac OS X

Character devices like PTY are only well supported since Mavericks (Mac OS 10.9). They are not supported at all on Mac OS 10.5 and older.

需要Mavericks(MacOS 10.9)以上版本才能很好支持类似PTY字符设备。不能在MacOS 10.5及之前的版本中支持。


On Mac OS 10.6, 10.7 and 10.8, the default event loop is SelectorEventLoop which uses selectors.KqueueSelectorselectors.KqueueSelectordoes not support character devices on these versions. The SelectorEventLoop can be used with SelectSelector or PollSelector to support character devices on these versions of Mac OS X. Example:

在MacOS 10.6,10.7和10.8,默认的事件循环是SelectorEventLoop,它使用selectors.KqueueSelector 。 

selectors.KqueueSelector在这些版本不支持字符设备。可以SelectorEventLoop结合SelectSelector 或PollSelector用于支持这些版本的字符设备。例如:


import asyncio
import selectors
selector = selectors.SelectSelector()
loop = asyncio.SelectorEventLoop(selector)
asyncio.set_event_loop(loop)


18.5.2.4. Event loop policies and the default policy 事件循环规则和默认规则


Event loop management is abstracted with a policy pattern, to provide maximal flexibility for custom platforms and frameworks. Throughout the execution of a process, a single global policy object manages the event loops available to the process based on the calling context. A policy is an object implementing the AbstractEventLoopPolicy interface.


For most users of asyncio, policies never have to be dealt with explicitly, since the default global policy is sufficient.


The default policy defines context as the current thread, and manages an event loop per thread that interacts with asyncio. The module-level functionsget_event_loop() and set_event_loop() provide convenient access to event loops managed by the default policy.


18.5.2.5. Event loop policy interface 事件循环规则接口

An event loop policy must implement the following interface:


18.5.2.6. Access to the global loop policy 访问全局循环规则


--结束END--

本文标题: Python Tulip ( async

本文链接: https://www.lsjlt.com/news/187374.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python Tulip ( async
    18.5.2.1. Event loop functions 事件循环函数The following functions are convenient shortcuts to accessing the methods of the gl...
    99+
    2023-01-31
    Python Tulip async
  • Python async/await
    原文链接:http://stackabuse.com/python-async-await-tutorial/hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao...
    99+
    2023-01-31
    Python async await
  • Python异步编程Async/Awai
    python 从3.5开始从语言层面提供了新的异步编程语法。 import asyncio async def hello(): print("hello the world") r = await asyncio....
    99+
    2023-01-31
    Python Async Awai
  • Python async+request与async+aiohttp实现异步网络请求探索
    目录前言初始环境准备搭建测试用的后端1.threading requests2.async requests3.async aiohttp前言 在学习协程的时候,会有一个疑问,使用协...
    99+
    2024-04-02
  • Python async模块如何使用
    这篇文章主要介绍“Python async模块如何使用”,在日常操作中,相信很多人在Python async模块如何使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Python ...
    99+
    2023-07-05
  • Python async模块使用方法杂谈
    目录一、什么是 generator(生成器)二、使用asyncio 实现异步io三、aiohttp提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 协程:协程(Cor...
    99+
    2023-05-14
    Python async原理 Python async模块
  • Python中Async语法协程的实现
    目录前言1.传统的Sync语法请求例子2.异步的请求3.基于生成器的协程3.1生成器3.2用生成器实现协程前言 在io比较多的场景中, Async语法编写的程序会以更少的时...
    99+
    2024-04-02
  • python实习总结(yeild,async,azwait和协程)
    目录一、yield使用简析二、async和await的使用1.什么是进程、协程、异步?2.如何处理200W数量的url,把所有的url保存下来?3.使用async的await和gat...
    99+
    2024-04-02
  • @Async失效情况
    1、注解@Async的方法不是public方法 2、注解@Async的返回值只能为void或者Future 3、注解@Async方法使用static修饰也会失效 4、spring无法扫描到异步类,没加注解@Async  或 @EnableA...
    99+
    2023-09-05
    java 开发语言
  • async是es7的吗
    这篇文章主要介绍“async是es7的吗”,在日常操作中,相信很多人在async是es7的吗问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”async是es7的吗”的疑惑有所帮助!接下来,请跟着小编一起来学习吧...
    99+
    2023-07-05
  • Python3 异步IO--async/
    Python3 异步IO--async/await 用asyncio提供的@asyncio.coroutine可以把一个generator标记为coroutine类型,然后在coroutine内部用yield from调用另一个corout...
    99+
    2023-01-31
    IO async
  • nodejs中async怎么用
    今天小编给大家分享一下nodejs中async怎么用的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解...
    99+
    2024-04-02
  • Golang协程与 async/await
    go中协程和 async/await 是并发原语,协程是轻量级执行线程,async/await 是语法糖,允许异步代码编写。协程在 goroutine 中运行,使用 go 关键字创建。a...
    99+
    2024-04-15
    协程 golang 并发请求
  • 详解Python中Sync与Async执行速度快慢对比
    目录前记1.一个简单的例子2.一个io的例子3.总结前记 Python新的版本中支持了async/await语法, 很多文章都在说这种语法的实现代码会变得很快, 但是这种快是有场景限...
    99+
    2023-03-01
    Python Sync Async执行速度对比 Python Sync Async执行速度 Python Sync Async
  • SpringBoot如何使用@Async
    这篇文章主要为大家展示了“SpringBoot如何使用@Async”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“SpringBoot如何使用@Async”这篇文章吧。SpringBoot使用@A...
    99+
    2023-06-22
  • 怎么使用Async函数
    本篇内容介绍了“怎么使用Async函数”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Async 函数是一个...
    99+
    2024-04-02
  • javascript中的async怎么用
    随着 Web 应用程序越来越复杂,异步编程变得越来越重要。在 JavaScript 中,我们可以使用 async/await 关键字来管理异步操作。本文将介绍 async 的基本用法,并提供一些实例来帮助你更好地理解。什么是 async?a...
    99+
    2023-05-14
  • async和await怎么使用
    本篇内容介绍了“async和await怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!Eric:如何讲清楚Promiseasync和a...
    99+
    2023-06-03
  • requests-html async异步使用
    requests-html async的使用示例代码:from requests_html import AsyncHTMLSession asession = AsyncHTMLSess...
    99+
    2023-01-31
    requests html async
  • async和await实例分析
    本文小编为大家详细介绍“async和await实例分析”,内容详细,步骤清晰,细节处理妥当,希望这篇“async和await实例分析”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。创建static voi...
    99+
    2023-06-17
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作