Linux 事件驱动和异步有什么区别?在 epoll 和 AIO 之间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5844955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between event-driven and asynchronous? Between epoll and AIO?
提问by Continuation
Event-driven and asynchronous are often used as synonyms. Are there any differences between the two?
事件驱动和异步通常用作同义词。两者之间有什么区别吗?
Also, what is the difference between epoll
and aio
? How do they fit together?
此外,之间有什么区别epoll
和aio
?它们是如何组合在一起的?
Lastly, I've read many times that AIO in Linux is horribly broken. How exactly is it broken?
最后,我读过很多次 Linux 中的 AIO 严重损坏。究竟是怎么破的?
Thanks.
谢谢。
采纳答案by c-smile
Events is one of the paradigms to achieve asynchronous execution. But not all asynchronous systems use events. That is about semantic meaning of these two - one is super-entity of another.
事件是实现异步执行的范式之一。但并非所有异步系统都使用事件。那是关于这两个的语义含义 - 一个是另一个的超实体。
epoll and aio use different metaphors:
epoll 和 aio 使用不同的比喻:
epoll is a blocking operation (epoll_wait()
) - you block the thread until some event happens and then you dispatch the event to different procedures/functions/branches in your code.
epoll 是一个阻塞操作 ( epoll_wait()
) - 阻塞线程直到某个事件发生,然后将事件分派到代码中的不同过程/函数/分支。
In AIO, you pass the address of your callback function (completion routine) to the system and the system calls your function when something happens.
在 AIO 中,您将回调函数(完成例程)的地址传递给系统,系统会在发生某些事情时调用您的函数。
Problem with AIO is that your callback function code runs on the system thread and so on top of the system stack. A few problems with that as you can imagine.
AIO 的问题在于您的回调函数代码在系统线程上运行,并在系统堆栈顶部运行。你可以想象到一些问题。
回答by neebz
Event driven is a single thread where events are registered for a certain scenario. When that scenario is faced, the events are fired. However even at that time each of the events are fired in a sequential manner. There is nothing Asynchronous about it. Node.js (webserver) uses events to deal with multiple requests.
事件驱动是单个线程,其中为特定场景注册事件。当遇到这种情况时,将触发事件。然而,即使在那个时候,每个事件都是按顺序触发的。没有什么是异步的。Node.js(网络服务器)使用事件来处理多个请求。
Asynchronous is basically multitasking. It can spawn off multiple threads or processes to execute a certain function. It's totally different from event driven in the sense that each thread is independent and hardly interact with the main thread in an easy responsive manner. Apache (webserver) uses multiple threads to deal with incoming requests.
异步基本上是多任务处理。它可以产生多个线程或进程来执行某个功能。它与事件驱动完全不同,因为每个线程都是独立的,并且几乎不会以简单的响应方式与主线程交互。Apache(网络服务器)使用多个线程来处理传入的请求。
回答by Stephen Chung
They are completely different things.
它们是完全不同的东西。
The events-driven paradigm means that an object called an "event" is sent to the program whenever something happens, without that "something" having to be polled in regular intervals to discover whether it has happened. That "event" may be trapped by the program to perform some actions (i.e. a "handler") -- either synchronous or asynchronous.
事件驱动范式意味着每当发生某事时,都会将一个称为“事件”的对象发送到程序,而不必定期轮询该“某事”以发现它是否已发生。该“事件”可能会被程序捕获以执行某些操作(即“处理程序”)——同步或异步。
Therefore, handling of events can either be synchronous or asynchronous. JavaScript, for example, uses a synchronous eventing system.
因此,事件的处理可以是同步的,也可以是异步的。例如,JavaScript 使用同步事件系统。
Asynchronous means that actions can happen independent of the current "main" execution stream. Mind you, it does NOTmean "parallel", or "different thread". An "asynchronous" action may actually run on the main thread, blocking the "main" execution stream in the meantime. So don't confuse "asynchronous" with "multi-threading".
异步意味着动作可以独立于当前的“主”执行流而发生。你要知道,它确实不是意味着“平行”或“不同的线程”。“异步”操作实际上可能在主线程上运行,同时阻塞“主”执行流。所以不要将“异步”与“多线程”混淆。
You may say that, technically speaking, an asynchronous operation automatically assumeseventing -- at least "completed", "faulted" or "aborted/cancelled" events (one or more of these) are sent to the instigator of the operation (or the underlying O/S itself) to signal that the operation has ceased. Thus, async is always event-driven, but not the other way round.
您可能会说,从技术上讲,异步操作会自动假设事件——至少“完成”、“故障”或“中止/取消”事件(其中一个或多个)被发送到操作的发起者(或底层操作系统本身)以表示操作已停止。因此,异步始终是事件驱动的,而不是相反。
回答by Anon
Lastly, I've read many times that AIO in Linux is horribly broken. How exactly is it broken?
最后,我读过很多次 Linux 中的 AIO 严重损坏。究竟是怎么破的?
AIO as done via KAIO/libaio
/io_submit
comes with a lot of caveatsand is tricky to use well if you want it to behave rather than silentlyblocking (e.g. only works on certain types of fd, when using files/block devices only actually works for direct I/O but those are the tip of the iceberg). It did eventually gain the ability to indicate file descriptor readiness with the 4.19 kernel) which is useful for programs using sockets.
AIO如通过KAIO做/ libaio
/io_submit
自带了很多需要注意的地方,是棘手的,如果你想它的行为,而不是使用以及默默使用文件时阻塞(如只适用于某些类型的FD,中/块设备实际上只适用于直接I/O,但这些只是冰山一角)。它最终获得了使用 4.19 内核指示文件描述符准备就绪的能力,这对于使用套接字的程序很有用。
POSIX AIO on Linux is actually a userspace threads implementation by glibcand comes with its own limitations (e.g. it's considered slow and doesn't scale well).
Linux 上的 POSIX AIO 实际上是 glibc 的用户空间线程实现,并且有其自身的局限性(例如,它被认为很慢并且不能很好地扩展)。
These days (2020) hope for doing arbitrary asynchronous I/O on Linux with less pain and tradeoffs is coming from io_uring
...
如今(2020 年)希望在 Linux 上以更少的痛苦和权衡来执行任意异步 I/O 来自io_uring
......