python中的异步编程 [英] asynchronous programming in python

查看:22
本文介绍了python中的异步编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 中是否有异步编程的通用概念?我可以为一个函数分配一个回调函数,执行它并立即返回主程序流程,无论该函数执行需要多长时间?

Is there a generic notion of asynchronous programming in python? Could I assign a callback to a function, execute it and return to the main program flow immediately, no matter how long the execution of that function would take?

推荐答案

看这里:

Python 中的异步编程

异步编程和 Twisted 简介

值得一试:

asyncio(以前称为 Tulip)已签入 Python 默认分支

今天 Python 拥有 asyncIO — 异步 I/O、事件循环、协程和任务 内置.

Today Python has asyncIO — Asynchronous I/O, event loop, coroutines and tasks built in.

来自上面链接的描述:

asyncIO 模块提供用于编写​​单线程使用协程的并发代码,通过套接字多路复用 I/O 访问和其他资源,运行网络客户端和服务器,以及其他相关的原语.这是更详细的包列表内容:

The asyncIO module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives. Here is a more detailed list of the package contents:

  1. 具有各种特定于系统的实现的可插拔事件循环;
  2. 传输和协议抽象(类似于 Twisted 中的那些);
  3. 具体支持 TCP、UDP、SSL、子进程管道、延迟调用、和其他(有些可能依赖于系统);
  4. 模拟 concurrent.futures 模块中的 Future 类,但适用于事件循环;
  5. 基于从 (PEP 380) 到帮助以顺序方式编写并发代码;
  6. 取消对 Futures 和协程的支持;
  7. 使用的同步原语在单个线程中的协程之间,模仿那些线程模块;
  8. 用于将工作传递给线程池的接口,有时你绝对必须使用一个库进行阻塞 I/O 调用.

异步编程更复杂比经典的顺序"编程:参见 Develop with asyncio页面,其中列出了常见的陷阱并解释了如何避免它们.使能够开发过程中的调试模式以检测常见问题.

Asynchronous programming is more complex than classical "sequential" programming: see the Develop with asyncio page which lists common traps and explains how to avoid them. Enable the debug mode during development to detect common issues.

也值得一试:

异步编程指南带有异步IO的Python

这篇关于python中的异步编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆