阻塞 IO 与非阻塞 IO;寻找好文章 [英] Blocking IO vs non-blocking IO; looking for good articles

查看:24
本文介绍了阻塞 IO 与非阻塞 IO;寻找好文章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾几何时,我偶然看到了Introduction to Indy 文章和从那时起就不停地思考阻塞与非阻塞 IO.

Once upon a time I bumped into Introduction to Indy article and can't stop thinking about blocking vs non-blocking IO ever since then.

寻找一些描述阻塞 IO 和非阻塞 IO 的优缺点以及如何在每种情况下设计应用程序以获得自然、易于理解和易于维护的代码的好文章.
想了解大图...

Looking for some good articles describing what are pros and cons of blocking IO and non-blocking IO and how to design your application in each case to get natural, easy to understand and easy to maintain code.
Would like to understand BIG picture...

推荐答案

阻塞 IO 意味着在完全接收到 IO 之前,给定的线程不能再做任何事情(在套接字的情况下,此等待可能需要很长时间).

Well blocking IO means that a given thread cannot do anything more until the IO is fully received (in the case of sockets this wait could be a long time).

非阻塞 IO 意味着 IO 请求立即排队,函数返回.然后内核会在稍后的某个时间处理实际的 IO.

Non-blocking IO means an IO request is queued straight away and the function returns. The actual IO is then processed at some later point by the kernel.

对于阻塞 IO,您要么需要接受将等待每个 IO 请求,要么需要为每个请求触发一个线程(这会很快变得非常复杂).

For blocking IO you either need to accept that you are going to wait for every IO request or you are going to need to fire off a thread per request (Which will get very complicated very quickly).

对于非阻塞 IO,您可以发送多个请求,但您需要记住,直到某个稍后"点数据才会可用.检查数据是否实际到达可能是最复杂的部分.

For non-blocking IO you can send off multiple requests but you need to bear in mind that the data will not be available until some "later" point. This checking that the data has actually arrived is probably the most complicated part.

在 99% 的应用程序中,您无需关心 IO 阻塞.然而,有时您需要额外的性能,允许自己发起 IO 请求,然后在返回之前执行其他操作,并希望发现 IO 请求已完成.

In 99% of applications you will not need to care that your IO blocks. Sometimes however you need the extra performance of allowing yourself to initiate an IO request and then do something else before coming back and, hopefully, finding that the IO request has completed.

无论如何,只是我的 tuppence.

Anyway, just my tuppence.

编辑:要回答如何设计一个应用程序来处理阻塞 IO 同时具有良好的性能,协程 可能很合适.

Edit: To answer how to design an application for handling blocking IO while have good performance, coroutines could be a good fit.

这篇关于阻塞 IO 与非阻塞 IO;寻找好文章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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