异步和同步术语

发布于 2024-11-30 23:13:00 字数 794 浏览 7 评论 0原文

当与编程相关时,我对异步这个术语感到困惑。它在编程术语中的含义似乎与字典中的定义相反。例如,单词同步表示:

  1. 同时发生;时间一致;同时期的; 同时进行。

  2. 以相同的速度并且完全一起进行;一起重复出现。

然而,维基百科说:

“在编程中,异步事件是发生的事件 独立于主程序流程。异步操作是 以非阻塞方案执行的操作,允许主程序 流继续处理。”

难道不是“非阻塞”并且允许“主程序流继续处理”的东西被同步或“同时发生”吗?似乎术语“同步”意味着“非阻塞” -阻塞”和异步“阻塞”。为什么这些术语在与编程相关时似乎是相反使用的,或者它与我不理解的较低级计算有关?

当我使用同步 AJAX 时打电话,我做接下来...

$.ajax({
  url: somefile.php,
  async: false,
  success: {
    ...code that gets run on success...
  }
});

...code that gets run after the ajax-success code runs...

这样,它实际上会在运行脚本的其余部分之前等待响应,这是一个阻塞操作。那么为什么这被称为同步,当它不与任何其他进程同步时,但实际上相反呢?

I'm confused by the term asynchronous when related to programming. It seems to mean the opposite in programming terms as what it is defined as in the dictionary. For example, the word synchronous means:

  1. occurring at the same time; coinciding in time; contemporaneous;
    simultaneous.

  2. going on at the same rate and exactly together; recurring together.

Yet, Wikipedia says:

"In programming, asynchronous events are those occurring
independently of the main program flow. Asynchronous actions are
actions executed in a non-blocking scheme, allowing the main program
flow to continue processing."

Wouldn't something that is "non-blocking" and that allows "the main program flow to continue processing," be synchronized or "occurring at the same time"? It seems like the term synchronous suggests "non-blocking" and asynchronous, "blocking." Why do the terms appear to be used in reverse when related to programming, or does it have something to do with lower-level computing that I don't understand?

When I use an synchronous AJAX call, I do the following...

$.ajax({
  url: somefile.php,
  async: false,
  success: {
    ...code that gets run on success...
  }
});

...code that gets run after the ajax-success code runs...

With this, it actually waits for a response before running the rest of the script, it's a blocking action. Then why is this termed synchronous, when it's not synchronized with any other process, but actually the opposite?

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

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

发布评论

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

评论(5

鹊巢 2024-12-07 23:13:00

事实上,这是其中一种情况,该词的原始含义被颠覆,并且意味着与流行用法不同的东西。

电信中的“同步”意味着接收器在准备好接收消息时发出信号,只有在发出信号后发射器才会开始传输。当发送方处理完消息后,它会发出已完成的信号,以便接收方现在可以处理收到的消息并执行下一步应该执行的操作。

这当然是一种简化,而且非常广泛,但它应该让您感觉到 JS 中“(a)同步”的含义来自何处。

所以JS中的同步请求实际上是与程序主流程同步的。该程序向服务器发送请求(“我准备好接收”)并等待消息。来自服务器的消息将有一个明确定义的结尾(“消息在此结束 - 完成你的工作”)。当收到它时,JS 知道它可以继续执行程序。

Indeed, it's one of these cases, where original meaning of the word was subverted and means something different than in popular usage.

'Synchronisation' in telecommunication means that receiver signals whenever it is ready to receive messages, and only after this signal the transmitter will start transmitting. When the transmitter is done with the message, it will signal it has finished, so that receiver can now process the received message and do whatever it is supposed to be doing next.

This is of course a simplification and a very broad one, but it should give you the feeling of from where the meaning of '(a)synchronous' comes in JS.

So synchronous request in JS is actually synchronised with the main flow of the program. The program sends request to server ('I'm ready to receive') and waits for message. Message from the server will have a well defined end ('the message ends here - do your job'). When it is received, JS knows it can continue with execution of the program..

夕嗳→ 2024-12-07 23:13:00

同步:- 当每个任务连接并依赖于前一个任务时
任务

异步:- 每个任务独立于其他任务。

synchronous:- when each task connected and depend on the previous
task

asynchronous:- each task independent from others.

最冷一天 2024-12-07 23:13:00

在您的问题中,同步意味着两个部分正在相互等待。
例如,如果您有一个客户端代码向服务器发出请求,并且您的代码在服务器响应到达之前不会继续处理,那么这意味着您的代码是同步的,即与服务器的响应同步。
如果您的客户端代码发出请求但不等待响应并继续处理,一旦来自服务器的请求的响应到达,您的代码(例如在特定处理程序中)开始处理响应,然后处理是异步,即核心客户端处理与服务器的响应是异步的。
这些术语意味着缺乏依赖性,因为如果(使用客户端-服务器的示例)客户端必须强制响应才能继续其处理,则不可能将同步代码转换为异步代码。

不会是“非阻塞”并且允许“主要
程序流继续处理,“被同步或”发生在
同时”?同步一词似乎表明
“非阻塞”和异步“阻塞”。

对术语的解释错误。同步的不是程序流程。不同部分可以(例如线程)或属于同一程序,可以同步或不同步。

Synchronous in the context of your question means that 2 parts are waiting for each other.
For example if you have a client code that makes a request to a server and your code does not continue its processing until the response of the server arrives, then this means that your code is synchronous i.e. synchronous with the response of the server.
If your client code makes the request but does not wait for a response and continues its processing and once the response of the request from the server arrives, then your code (in a specific handler for instance) starts to process the response then the handling is asynchronous i.e. the core client processing is asynhronous with the reponse of the server.
These terms imply some short of dependency since it is not possible to convert a synchronous code to asynchronous if (using the example of client-server) the response is mandatory for the client to continue its processing.

Wouldn't something that is "non-blocking" and that allows "the main
program flow to continue processing," be synchronized or "occurring at
the same time"? It seems like the term synchronous suggests
"non-blocking" and asynchronous, "blocking."

Wrong interpretation of the terms. It is not the program flow that synchronized. It is different parts that could be (e.g. threads) or could not be part of the same program that could be synchronized or not.

墟烟 2024-12-07 23:13:00

同步请求:当一个请求在执行另一个请求之前等待该特定请求的响应时,该请求称为同步请求。即,当客户端同步进行调用时,它会阻止客户端浏览器,以确保客户端在获得前一个调用的服务器响应之前无法进行另一个调用。

异步请求:异步调用独立工作,即在执行另一个调用或请求之前它不会等待服务器响应。所以你可以简单地同时进行不同的调用,而无需等待服务器响应。

Synchronous Request: A request is called synchronous when it waits for the response of that particular request before executing the other one. i.e when a client makes a call synchronously then it blocks the client browser to ensure that client couldn’t make another call before getting the server response for that previous call.

Asynchronous Request: An asynchronous call works independently i.e it doesn’t wait for the server response before executing another call or request. so u can simply make different calls at the same time without waiting for the server response.

时光磨忆 2024-12-07 23:13:00

同步使浏览器在请求完成之前没有响应,即它将等待直到请求得到服务,而异步将允许浏览器在等待响应的同时继续处理。

synchronous makes the browser unresponsive until the request is complete i.e to say it will wait until the request is served, while asynchronous will allow the browser to continue processing while waiting for a response.

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