并行编程与异步编程的比较

发布于 2024-11-27 17:48:15 字数 156 浏览 0 评论 0原文

并行编程与异步编程有何不同?

我知道异步编程用于在后台线程/工作人员中完成工作或等待 像 I/O 这样需要完成的事情。

*并行任务也可以是异步任务吗?

*异步任务可以并行完成吗?

让我有点困惑。

以上有什么例子吗?

How is parallel programming different with respect to Asynchronous programming?

I know async programming is used to do work in background threads/workers or waiting for
something to finish like I/O.

*Can task(s) in parallel be also task(s) in async?

*Can task(s) in async be done in parallel?

Bit confusing for me.

Any examples for the above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人海汹涌 2024-12-04 17:48:15

多线程、多进程和异步编程都是并发技术,您可以在一个程序中一次完成多件事。如果您只有一个处理器/机器,那么这些都不是真正的“并行”。它们都只是以管道方式执行某些代码,以便您“感觉”它们都在一起执行。

前两个依赖于您的 CPU 切换任务。您只需以某种方式告诉计算机这些是您想要完成的事情,然后让它决定如何为各种任务分配时间和其他资源。我们将在这里掩盖线程和进程之间的差异。

异步编程意味着您的应用程序控制您想要完成的这些任务的切换。一个简单的例子是当您想要读取/写入 2 个 I/O 通道时。您的应用程序可以将数据发送到 1,直到 2 有可用数据。当它发生时,它会从 2 读取数据,然后恢复发送到 1,然后切换到向 2 发送数据。这个想法是,等到有一些事件需要服务,然后根据情况在事件之间切换可用性和需求。从某种意义上说,您正在使用此类应用程序手动进行进程调度。优点之一是不存在与多个进程/线程相关的系统开销。许多异步例程依赖于 select 系统调用。

Multi threaded, Multi process and asynchronous programming are all concurrency techniques whereby you can get more than one thing done at a time in a single program. If you have just a single processor/machine, none of these are truly "parallel". They all just pipeline the execution of some code so that you "feel" like they're all executing together.

The first two rely on the CPU switching tasks for you. You simply tell the computer somehow that these are the things you want done and then let it decide how to allocate time and other resources to the various tasks. We'll gloss over the differences between threads and processes here.

Asynchronous programming means that your application controls the switching of these tasks you want done. An crude example is when there are 2 I/O channels you want to read/write to. Your application can sort of send data to 1 till 2 has data available on it. When it does, it will read out the data from 2 and then resume sending to 1 and then switch to sending data to 2. The idea is that you wait till there are some events that need to be serviced and then switch between events as per availability and need. In some sense, you're manually doing the process scheduling with these kinds of apps. One advantage is that the system overhead associated with multiple processes/threads is not there. Many asynchronous routines rely on the select system call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文