异步调用和非阻塞调用之间的区别是什么?在阻塞和同步调用之间(请提供示例)?
当前回答
在许多情况下,它们是同一事物的不同名称,但在某些情况下,它们是完全不同的。这要看情况。在整个软件行业中,术语的应用并不是完全一致的。
例如,在经典的套接字API中,非阻塞套接字会立即返回一个特殊的“将阻塞”错误消息,而阻塞套接字则会阻塞。您必须使用单独的函数,如选择或轮询,以确定何时是重试的最佳时间。
但是异步套接字(Windows套接字支持)或。net中使用的异步IO模式更方便。您调用一个方法来启动一个操作,当操作完成时,框架会回调您。即使在这里,也有基本的区别。异步Win32套接字通过传递Window消息将结果“封送”到特定的GUI线程上,而. net异步IO是自由线程的(你不知道你的回调将被调用在哪个线程上)。
所以它们的意思并不总是一样的。要提取套接字示例,我们可以这样说:
Blocking and synchronous mean the same thing: you call the API, it hangs up the thread until it has some kind of answer and returns it to you. Non-blocking means that if an answer can't be returned rapidly, the API returns immediately with an error and does nothing else. So there must be some related way to query whether the API is ready to be called (that is, to simulate a wait in an efficient way, to avoid manual polling in a tight loop). Asynchronous means that the API always returns immediately, having started a "background" effort to fulfil your request, so there must be some related way to obtain the result.
其他回答
同步意味着按顺序在另一个结果之后启动一个。
异步意味着一起开始,结果上不保证顺序
阻塞是指某些东西导致执行下一步的阻碍。
无阻塞是指不等待任何东西就继续运行,克服障碍。
我敲门,等着他们开门。(我在这里无所事事)
我敲门,如果他们马上开门,我就和他们打招呼,然后走进去等等。如果门没有立刻打开,我就去下一户人家敲门。(我正在做某事,没有闲着)
只有在下雨的时候我才会出去。(依赖关系存在)
我要出去。可能会下雨。(独立事件,发生时间无关紧要)
同步或异步,两者都可以是阻塞或非阻塞的,反之亦然
synchronous | asynchonous | |
---|---|---|
block | Block I/O must be a synchronus I/O, becuase it has to be executed in order. Synchronous I/O might not be block I/O | Not exist |
non-block | Non-block and Synchronous I/O at the same time is polling/multi-plexing.. | Non-block and Asynchronous I/O at the same time is parallel execution, such as signal trigger… |
block/non-block描述了初始化实体本身的行为,它意味着实体在等待I/O完成期间所做的事情 同步/异步描述了I/O初始化实体和I/O执行器(例如操作系统)之间的行为,它意味着这两个实体是否可以并行执行
在许多情况下,它们是同一事物的不同名称,但在某些情况下,它们是完全不同的。这要看情况。在整个软件行业中,术语的应用并不是完全一致的。
例如,在经典的套接字API中,非阻塞套接字会立即返回一个特殊的“将阻塞”错误消息,而阻塞套接字则会阻塞。您必须使用单独的函数,如选择或轮询,以确定何时是重试的最佳时间。
但是异步套接字(Windows套接字支持)或。net中使用的异步IO模式更方便。您调用一个方法来启动一个操作,当操作完成时,框架会回调您。即使在这里,也有基本的区别。异步Win32套接字通过传递Window消息将结果“封送”到特定的GUI线程上,而. net异步IO是自由线程的(你不知道你的回调将被调用在哪个线程上)。
所以它们的意思并不总是一样的。要提取套接字示例,我们可以这样说:
Blocking and synchronous mean the same thing: you call the API, it hangs up the thread until it has some kind of answer and returns it to you. Non-blocking means that if an answer can't be returned rapidly, the API returns immediately with an error and does nothing else. So there must be some related way to query whether the API is ready to be called (that is, to simulate a wait in an efficient way, to avoid manual polling in a tight loop). Asynchronous means that the API always returns immediately, having started a "background" effort to fulfil your request, so there must be some related way to obtain the result.
阻塞:在原语(同步或异步)处理完成后,控制返回到调用进程
非阻塞:调用后控制权立即返回进程
同步/异步是用来描述两个模块之间的关系。 阻塞/非阻塞是描述一个模块的情况。
一个例子: 模块X: I。 模块Y:“书店”。 X问Y:你有《c++入门》这本书吗?
blocking: before Y answers X, X keeps waiting there for the answer. Now X (one module) is blocking. X and Y are two threads or two processes or one thread or one process? we DON'T know. non-blocking: before Y answers X, X just leaves there and do other things. X may come back every two minutes to check if Y has finished its job? Or X won't come back until Y calls him? We don't know. We only know that X can do other things before Y finishes its job. Here X (one module) is non-blocking. X and Y are two threads or two processes or one process? we DON'T know. BUT we are sure that X and Y couldn't be one thread. synchronous: before Y answers X, X keeps waiting there for the answer. It means that X can't continue until Y finishes its job. Now we say: X and Y (two modules) are synchronous. X and Y are two threads or two processes or one thread or one process? we DON'T know. asynchronous: before Y answers X, X leaves there and X can do other jobs. X won't come back until Y calls him. Now we say: X and Y (two modules) are asynchronous. X and Y are two threads or two processes or one process? we DON'T know. BUT we are sure that X and Y couldn't be one thread.
请注意上面两个加粗的句子。为什么题目中的粗体字包含两种情况,而题目中的粗体字只包含一种情况?这是区分非阻塞和异步的关键。
让我试着用另一种方式来解释这四个词:
blocking: OMG, I'm frozen! I can't move! I have to wait for that specific event to happen. If that happens, I would be saved! non-blocking: I was told that I had to wait for that specific event to happen. OK, I understand and I promise that I would wait for that. But while waiting, I can still do some other things, I'm not frozen, I'm still alive, I can jump, I can walk, I can sing a song etc. synchronous: My mom is gonna cook, she sends me to buy some meat. I just said to my mom: We are synchronous! I'm so sorry but you have to wait even if I might need 100 years to get some meat back... asynchronous: We will make a pizza, we need tomato and cheeze. Now I say: Let's go shopping. I'll buy some tomatoes and you will buy some cheeze. We needn't wait for each other because we are asynchronous.
下面是一个关于非阻塞和同步的典型例子:
// thread X
while (true)
{
msg = recv(Y, NON_BLOCKING_FLAG);
if (msg is not empty)
{
break;
}
else
{
sleep(2000); // 2 sec
}
}
// thread Y
// prepare the book for X
send(X, book);
You can see that this design is non-blocking (you can say that most of time this loop does something nonsense but in CPU's eyes, X is running, which means that X is non-blocking. If you want you can replace sleep(2000) with any other code) whereas X and Y (two modules) are synchronous because X can't continue to do any other things (X can't jump out of the loop) until it gets the book from Y. Normally in this case, making X blocking is much better because non-blocking spends much resource for a stupid loop. But this example is good to help you understand the fact: non-blocking doesn't mean asynchronous.
这四个字确实很容易让我们困惑,我们应该记住的是,这四个字是为建筑设计服务的。学习如何设计一个好的架构是区分它们的唯一方法。
例如,我们可以设计这样一种架构:
// Module X = Module X1 + Module X2
// Module X1
while (true)
{
msg = recv(many_other_modules, NON_BLOCKING_FLAG);
if (msg is not null)
{
if (msg == "done")
{
break;
}
// create a thread to process msg
}
else
{
sleep(2000); // 2 sec
}
}
// Module X2
broadcast("I got the book from Y");
// Module Y
// prepare the book for X
send(X, book);
在这个例子中,我们可以说
X1是非阻塞的 X1和X2是同步的 X和Y是异步的
如果需要,还可以用这四个字来描述在X1中创建的线程。
再说一次:这四个字是为建筑设计服务的。所以我们需要的是一个合适的架构,而不是像语言律师一样区分这四个词。如果你遇到一些情况,你不能很清楚地区分这四个词,你应该忘记这四个词,用你自己的话来描述你的架构。
所以更重要的事情是:我们什么时候使用同步而不是异步?什么时候用阻塞代替非阻塞?X1的阻塞性比非阻塞性更好吗?X和Y是同步的还是异步的?为什么Nginx是非阻塞的?Apache为什么阻塞?这些问题是你必须弄清楚的。
为了做出正确的选择,您必须分析您的需求并测试不同体系结构的性能。没有这样一种体系结构可以满足各种需求。
推荐文章
- 什么时候我应该在ASP中使用异步控制器。净MVC吗?
- 理解设置
- 如何使HTTP请求在PHP和不等待响应
- 反应-显示加载屏幕,而DOM是渲染?
- 如何正确地读取异步/等待文件?
- 使用Moq模拟单元测试的异步方法
- 如何使用JUnit来测试异步进程
- Kotlin协程中的启动/连接和异步/等待之间有什么区别
- CompletableFuture, Future和RxJava的Observable之间的区别
- 为什么要在c#中使用Task<T>而不是ValueTask<T> ?
- Asyncio。Gather vs asyncio.wait
- 同步调用异步方法
- 所有异步forEach回调完成后的回调
- 并发、并行和异步方法之间的区别是什么?
- 同步调用一个异步Javascript函数