异步编程如何在单线程编程模型中工作?

时间:2022-12-07 18:19:09

I was going through the details of node.jsand came to know that, It supports asynchronous programming though essentially it provides a single threaded model.

我讲了node的细节。jsand了解到,它支持异步编程,但本质上它提供了一个单线程模型。

How is asynchronous programming handled in such cases? Is it like runtime itself creates and manages threads, but the programmer cannot create threads explicitly? It would be great if someone could point me to some resources to learn about this.

在这种情况下如何处理异步编程?它是否像运行时本身创建和管理线程,而程序员不能显式地创建线程?如果有人能给我一些资源来了解这一点,那就太好了。

2 个解决方案

#1


74  

Say it with me now: async programming does not necessarily mean multi-threaded.

现在就跟我说:异步编程并不一定意味着多线程。

Javascript is a single-threaded runtime - you simply aren't able to create new threads in JS because the language/runtime doesn't support it.

Javascript是一个单线程运行时—您无法在JS中创建新线程,因为语言/运行时不支持它。

Frank says it correctly (although obtusely) In English: there's a main event loop that handles when things come into your app. So, "handle this HTTP request" will get added to the event queue, then handled by the event loop when appropriate.

Frank在英语中正确地说了(尽管是obtusely):在你的应用程序中有一个主事件循环来处理。所以,“处理这个HTTP请求”将被添加到事件队列中,然后在适当的时候由事件循环处理。

When you call an async operation (a mysql db query, for example), node.js sends "hey, execute this query" to mysql. Since this query will take some time (milliseconds), node.js performs the query using the MySQL async library - getting back to the event loop and doing something else there while waiting for mysql to get back to us. Like handling that HTTP request.

当您调用异步操作(例如,一个mysql db查询)时,节点。js发送“嘿,执行这个查询”到mysql。由于这个查询将花费一些时间(毫秒),节点。js使用MySQL async库执行查询——返回事件循环,在等待MySQL返回时做一些其他的事情。比如处理HTTP请求。

Edit: By contrast, node.js could simply wait around (doing nothing) for mysql to get back to it. This is called a synchronous call. Imagine a restaurant, where your waiter submits your order to the cook, then sits down and twiddles his/her thumbs while the chef cooks. In a restaurant, like in a node.js program, such behavior is foolish - you have other customers who are hungry and need to be served. Thus you want to be as asynchronous as possible to make sure one waiter (or node.js process) is serving as many people as they can.

编辑:相比之下,节点。js只需等待(什么都不做)mysql返回。这称为同步调用。想象一下,在一家餐馆里,你的服务员把你点的菜递给厨师,然后坐下来,在厨师做饭时拨弄他/她的拇指。在餐厅里,比如在节点里。js程序,这样的行为是愚蠢的-你有其他的顾客,他们饿了,需要服务。因此,您希望尽可能地保持异步,以确保有一个服务员(或节点)。js进程)正在为尽可能多的人服务。

Edit done

编辑完成

Node.js communicates with mysql using C libraries, so technically those C libraries could spawn off threads, but inside Javascript you can't do anything with threads.

节点。js使用C库与mysql通信,因此从技术上讲,这些C库可能会衍生出线程,但在Javascript中,您不能对线程做任何事情。

#2


8  

Ryan said it best: sync/async is orthogonal to single/multi-threaded. For single and multi-threaded cases there is a main event loop that calls registered callbacks using the Reactor Pattern. For the single-threaded case the callbacks are invoked sequentially on main thread. For the multi-threaded case they are invoked on separate threads (typically using a thread pool). It is really a question of how much contention there will be: if all requests require synchronized access to a single data structure (say a list of subscribers) then the benefits of having multiple threaded may be diminished. It's problem dependent.

Ryan说的最好:sync/async是正交于单/多线程的。对于单线程和多线程的情况,有一个主事件循环,使用反应器模式调用已注册的回调。对于单线程情况,回调将在主线程上按顺序调用。对于多线程情况,它们在单独的线程(通常使用线程池)上调用。这实际上是一个有多少争用的问题:如果所有请求都需要对单个数据结构(比如一个订阅者列表)进行同步访问,那么拥有多线程的好处可能会减少。它依赖的问题。

As far as implementation, if a framework is single threaded then it is likely using poll/select system call i.e. the OS is triggering the asynchronous event.

就实现而言,如果一个框架是单线程的,那么它很可能使用轮询/选择系统调用,即操作系统触发异步事件。

#1


74  

Say it with me now: async programming does not necessarily mean multi-threaded.

现在就跟我说:异步编程并不一定意味着多线程。

Javascript is a single-threaded runtime - you simply aren't able to create new threads in JS because the language/runtime doesn't support it.

Javascript是一个单线程运行时—您无法在JS中创建新线程,因为语言/运行时不支持它。

Frank says it correctly (although obtusely) In English: there's a main event loop that handles when things come into your app. So, "handle this HTTP request" will get added to the event queue, then handled by the event loop when appropriate.

Frank在英语中正确地说了(尽管是obtusely):在你的应用程序中有一个主事件循环来处理。所以,“处理这个HTTP请求”将被添加到事件队列中,然后在适当的时候由事件循环处理。

When you call an async operation (a mysql db query, for example), node.js sends "hey, execute this query" to mysql. Since this query will take some time (milliseconds), node.js performs the query using the MySQL async library - getting back to the event loop and doing something else there while waiting for mysql to get back to us. Like handling that HTTP request.

当您调用异步操作(例如,一个mysql db查询)时,节点。js发送“嘿,执行这个查询”到mysql。由于这个查询将花费一些时间(毫秒),节点。js使用MySQL async库执行查询——返回事件循环,在等待MySQL返回时做一些其他的事情。比如处理HTTP请求。

Edit: By contrast, node.js could simply wait around (doing nothing) for mysql to get back to it. This is called a synchronous call. Imagine a restaurant, where your waiter submits your order to the cook, then sits down and twiddles his/her thumbs while the chef cooks. In a restaurant, like in a node.js program, such behavior is foolish - you have other customers who are hungry and need to be served. Thus you want to be as asynchronous as possible to make sure one waiter (or node.js process) is serving as many people as they can.

编辑:相比之下,节点。js只需等待(什么都不做)mysql返回。这称为同步调用。想象一下,在一家餐馆里,你的服务员把你点的菜递给厨师,然后坐下来,在厨师做饭时拨弄他/她的拇指。在餐厅里,比如在节点里。js程序,这样的行为是愚蠢的-你有其他的顾客,他们饿了,需要服务。因此,您希望尽可能地保持异步,以确保有一个服务员(或节点)。js进程)正在为尽可能多的人服务。

Edit done

编辑完成

Node.js communicates with mysql using C libraries, so technically those C libraries could spawn off threads, but inside Javascript you can't do anything with threads.

节点。js使用C库与mysql通信,因此从技术上讲,这些C库可能会衍生出线程,但在Javascript中,您不能对线程做任何事情。

#2


8  

Ryan said it best: sync/async is orthogonal to single/multi-threaded. For single and multi-threaded cases there is a main event loop that calls registered callbacks using the Reactor Pattern. For the single-threaded case the callbacks are invoked sequentially on main thread. For the multi-threaded case they are invoked on separate threads (typically using a thread pool). It is really a question of how much contention there will be: if all requests require synchronized access to a single data structure (say a list of subscribers) then the benefits of having multiple threaded may be diminished. It's problem dependent.

Ryan说的最好:sync/async是正交于单/多线程的。对于单线程和多线程的情况,有一个主事件循环,使用反应器模式调用已注册的回调。对于单线程情况,回调将在主线程上按顺序调用。对于多线程情况,它们在单独的线程(通常使用线程池)上调用。这实际上是一个有多少争用的问题:如果所有请求都需要对单个数据结构(比如一个订阅者列表)进行同步访问,那么拥有多线程的好处可能会减少。它依赖的问题。

As far as implementation, if a framework is single threaded then it is likely using poll/select system call i.e. the OS is triggering the asynchronous event.

就实现而言,如果一个框架是单线程的,那么它很可能使用轮询/选择系统调用,即操作系统触发异步事件。