首页 \ 问答 \ Netty - 它是如何异步的?(Netty - how is it asynchronous?)

Netty - 它是如何异步的?(Netty - how is it asynchronous?)

让我们想象有一台服务器,当接收到一个汽车模型的请求时,询问所有寻找最便宜汽车经销商并且以价格回复(使用任何协议)的汽车经销商。 此操作需要一段时间。 在一个偶然阻塞的请求/响应服务器模型中,我确实如此

request = "audi a8" // prepare a request and one line after have the response
response = server.findCheapestCar(request) // takes 20 seconds

我不想阻止我的客户端主线程20秒,所以宁愿要它异步执行。 我对异步的理解是我可以传递某种对象并随身携带。 一旦服务器准备好响应,它将通知我传递的对象 - >休闲回调模式。

这种方法需要库匹配 - 客户端和服务器都需要知道对象。 但我希望在Netty上构建的异步服务器能够处理来自各种客户端(C ++ / Python和其他)的请求。

netty的异步性来自何处? 客户端需要什么来从异步获益?


Let's imagine there is a server, that when receives a request with a car model queries all known car dealers looking for the cheapest one and responds back with the price (using whatever protocol). This actions takes a while. In a casual blocking request/response server model, I do

request = "audi a8" // prepare a request and one line after have the response
response = server.findCheapestCar(request) // takes 20 seconds

I don't want to block my client main thread for 20 seconds, so would rather want it to be executed asynchronously. My understanding for something being asynchronous is that I can pass some sort of an object to it and carry one with my work. Once the server is ready with the response it will notify the object I passed -> Casual callback pattern.

This approach would require library match - both client ad the server need to know the object. But I want my asynchronous server built on Netty to be able to handle requests from various clients (C++/Python and others).

Where is the asynchronousness of netty coming from? What do I need on the client side to benefit from the asynchronousness?


原文:https://stackoverflow.com/questions/46831551
更新时间:2021-04-04 14:04

最满意答案

netty的异步来自哪里?

Netty采用了你可能从JavaScript这样的语言中得知的eventloops原理。 这允许netty完全异步工作。 (有关eventloops和基本原理的更多信息,我会推荐这个关于JavaScript中的evenloop的视频

客户端需要什么来从异步获益?

  1. 客户端发送请求(包含有效负载和请求id = clientside递增整数)
  2. 服务器处理请求50秒
  3. 服务器发送响应(包含有效载荷和客户端在其请求中发送的相同请求ID)
  4. 客户端收到响应并查找请求标识(如果客户端能够找到请求标识及其基础回调,它将调用它)

希望有所帮助


Where is the asynchronousness of netty coming from?

Netty adopted the principle of eventloops which you may known from a language like JavaScript. This allows netty to work fully asynchronous. (For more information about eventloops and the basic underlying principle I would recommend this video about the evenloop in JavaScript)

What do I need on the client side to benefit from the asynchronousness?

  1. Client sends request (containing payload and request id = clientside incrementing integer)
  2. Server process the request for 50sec
  3. Server sends response (containing the payload and the same request id the client send in his request)
  4. Client receives the response and looks up the request id (If the client is able to find the request id and its underlying callback it will invoke it)

Hope that helped

相关问答

更多