异步编程吐槽: Asyncio除了写网络IO之外一无是处

在仔细学习了官方文档、查阅了一些资料之后。

原因一:只适用网络IO

我困惑了,我觉得Asyncio库设计来就是为了sleep的,其他地方一无是处。

  • 首先,它仍然受制于GIL(GIL在IO的时候可以被释放、没错)

  • 第二,它设计的目的是让一个线程可以在多个协程之间协作(相当于线程的时间片资源再细分)

  • 仅在重IO的时候有效(且必须是网络IO,不可以是硬盘IO)。

  • 因为硬盘IO是阻塞的行为。在官方库中是通过asyncio.to_thread这个函数包装在另一个线程中执行的。那我为什么不直接用多线程?

事实确实如此。引用一段reddit老哥的评论。

总结他的话,很精辟。如果你写的不是socket 服务器/客户端,那你完全没必要用这个库。

目前相关的异步框架有aiohttp和tornado。异步框架擅长处理C10K问题,即相当于GO当中的select,用一个线程复用,在多个任务上轮询,保证每个任务中大量的sleep时间不会被浪费。

但是,恕我直言。为什么要用Python来处理高并发的框架?

原因二:要求整体异步风格,

还可以参考这篇:Why Python Developers Hate Asyncio

我引用几段我赞同的。

如果你使用asyncio,你必须使用整个异步编程框架,就像是病毒一样感染了整个程序

你几乎不可能需要使用到它。即使是再python服务器领域,绝大多数时间下多线程服务器完全能够满足需求(除了非常长的长链接)

原因三:并不是为了高性能设计的

在除了长链接这个场景,异步编程的性能都不如多线程。

还有,锁是用来做什么的?

是我在这个帖子中看到的。

这个库实现了在多线程中的锁、共享队列等等。这个帖子的老哥提出了这个疑问:

我都单线程了,我究竟有什么东西是需要用锁来保护的?

(我也写一个)我为啥需要一个专门的共享队列?

如果我的看法不对,欢迎指点。

以下是老哥原文:

There's very little useful about this library. It is misrepresented even in its own documentation.

What it actually does:

It utilizes a particular operating system API to perform networking I/O (at transport level, i.e. TCP or UDP), in an asynchronous way, where the I/O is scheduled and then the system is polled repeatedly to figure out if the scheduled I/O was completed.

In other words, asyncio is useless when it comes to files, or dealing with other kinds of I/O. asyncio is particularly useless when you aren't doing any I/O (i.e. you are trying to run some computations in parallel).

In yet another words: if you are writing anything other than a socket server / client, you have no business using this library.

What is advertised as:

And yet... this library is forced as some kind of solution for concurrent programming in Python. It's absurd, because it cannot achieve it in principle, and it's idiotic because the interface is a walking disaster. It is another stupid fashion trend in Python community, which resulted from some inferiority complex, where Python programmers feel bad for their language not having any decent tools to run simultaneous computations, and, specifically, async sounds hip, and has been recently added to a bunch of other languages.

So, not wanting to trail behind the fashion, Python added it too.


Don't feel bad for not understanding how asyncio works. It's a poorly designed library, that is also intentionally misleading about what it does. You don't lose anything of substance if you don't use this library at all. (If you want the same functionality, you can just use select module, with a much more straight-forward interface, and a lot less bloat).

Unfortunately, there are third-party libraries that rely on asyncio. And, sometimes, you have no choice but to embrace this nonsense, if you really need to use that other library... If that's the case, please except my condolences. You will have to read the documentation and go through examples, until you "get it". Enough has been written about it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值