简体   繁体   English

在高性能套接字上使用Async-Await vs ThreadPool与MultiThreading(C10k解决方案?)

[英]Async-Await vs ThreadPool vs MultiThreading on High-Performance Sockets (C10k Solutions?)

I'm really confused about async-await s, pool s and thread s. 我真的很困惑async-await s, pool s和thread s。 The main problem starts with this question: "What can I do when I have to handle 10k socket I/O?" 主要问题始于这个问题:“当我必须处理10k套接字I / O时,我该怎么办?” (aka The C10k Problem ). (又名C10k问题 )。

  • First, I tried to make a custom pooling architecture with threads that uses one main Queue and multiple Thread s to process all incoming datas. 首先,我尝试使用一个使用一个主Queue和多个Thread来处理所有传入数据的线程来创建自定义池化架构。 It was a great experience about understanding thread-safety and multi-threading but thread is an overkill with async-await nowadays. 对于理解thread-safetymulti-threading thread来说,这是一次很棒的体验,但是thread对于现在的async-await是一种过度杀伤力
  • Later, I implemented a simple architecture with async-await but I can't understand why "The async and await keywords don't cause additional threads to be created." 后来,我使用async-await实现了一个简单的架构,但我无法理解为什么“async和await关键字不会导致创建额外的线程。” ( from MSDN )? 来自MSDN )? I think there must be some thread s to do jobs like BackgroundWorker . 我认为必须有一些thread来做像BackgroundWorker这样的工作。
  • Finally, I implemented another architecture with ThreadPool and it looks like my first custom pooling. 最后,我使用ThreadPool实现了另一个架构,它看起来像我的第一个自定义池。

Now, I think there should be someone else with me who confused about handling The C10k . 现在,我认为我应该有其他人对处理C10k感到困惑。 My project is a dedicated (central) server for my game project that is hub/lobby server like MCSG 's lobbies or COD's matchmaking servers. 我的项目是我的游戏项目的专用(中央)服务器,它是集线器/大厅服务器,如MCSG的大厅或COD的配对服务器。 I'll do the login operations, game server command executions/queries and information serving (like version, patch). 我将进行登录操作,游戏服务器命令执行/查询和服务信息(如版本,补丁)。

Last part might be more specific about my project but I really need some good suggestions about real world solutions about multiple (heavy) data handling. 最后一部分可能对我的项目更具体,但我真的需要一些关于多个(重)数据处理的真实世界解决方案的好建议。

(Also yes, 1k-10k-100k connection handling depending on server hardware but this is a general question) (也就是说,1k-10k-100k连接处理取决于服务器硬件,但这是一般性问题)


The key point: Choosing Between the Task Parallel Library and the ThreadPool (MSDN Blog) 关键点: 在任务并行库和ThreadPool之间选择 (MSDN博客)


[ADDITIONAL] Good (basic) things to read who wants to understand what are we talking about: [附加]想要了解我们在谈论什么的好(基本)事物:

  1. Threads 主题
  2. Async, Await 异步,等待
  3. ThreadPool 线程池
  4. BackgroundWorker 的BackgroundWorker

async / await is roughly analogous to the "Serve many clients with each thread, and use asynchronous I/O and completion notification" approach in your referenced article. async / await大致类似于“为每个线程提供服务的许多客户端,并在您引用的文章中使用异步I / O和完成通知”方法。

While async and await by themselves do not cause any additional threads, they will make use of thread pool threads if an async method resumes on a thread pool context. 虽然asyncawait它们本身不会导致任何其他线程,但如果async方法在线程池上下文中恢复,它们将使用线程池线程。 Note that the async interaction with ThreadPool is highly optimized; 请注意,与ThreadPoolasync交互是高度优化的; it is very doubtful that you can use Thread or ThreadPool to get the same performance (with a reasonable time for development). 您可以使用ThreadThreadPool获得相同的性能(具有合理的开发时间)是非常值得怀疑的。

If you can, I'd recommend using an existing protocol - eg, SignalR. 如果可以的话,我建议使用现有协议 - 例如SignalR。 This will greatly simplify your code, since there are many ( many ) pitfalls to writing your own TCP/IP protocol. 这将大大简化您的代码,因为编写自己的TCP / IP协议有很多( 很多 )陷阱。 SignalR can be self-hosted or hosted on ASP.NET. SignalR可以自托管或托管在ASP.NET上。

No. If we use asynchronous programming pattern that .NET introduced in 4.5, in most of the cases we need not to create manual thread by us. 不。如果我们使用4.5中引入的.NET的异步编程模式,在大多数情况下我们不需要由我们创建手动线程。 The compiler does the difficult work that the developer used to do. 编译器完成了开发人员过去所做的艰巨工作。 Creating a new thread is costly, it takes time. 创建新线程代价高昂,需要时间。 Unless we need to control a thread, then “Task-based Asynchronous Pattern (TAP)” and “Task Parallel Library (TPL)” is good enough for asynchronous and parallel programming. 除非我们需要控制线程,否则“基于任务的异步模式(TAP)”和“任务并行库(TPL)”对于异步和并行编程来说已经足够了。 TAP and TPL uses Task. TAP和TPL使用Task。 In general Task uses the thread from ThreadPool(A thread pool is a collection of threads already created and maintained by .NET framework. If we use Task, most of the cases we need not to use thread pool directly. A thread can do many more useful things. You can read more about Thread Pooling 通常,Task使用ThreadPool中的线程(线程池是已经由.NET框架创建和维护的线程的集合。如果我们使用Task,大多数情况下我们不需要直接使用线程池。线程可以做更多有用的东西。你可以阅读更多关于线程池的信息

You can avoid performance bottlenecks and enhance the overall responsiveness of your application by using asynchronous programming. 通过使用异步编程,您可以避免性能瓶颈并提高应用程序的整体响应能力。 Asynchrony is essential for activities that are potentially blocking, such as when your application accesses the web. 异步对于可能阻塞的活动至关重要,例如当您的应用程序访问Web时。 Access to a web resource sometimes is slow or delayed. 访问Web资源有时会很慢或延迟。 If such an activity is blocked within a synchronous process, the entire application must wait. 如果在同步过程中阻止此类活动,则整个应用程序必须等待。 In an asynchronous process, the application can continue with other work that doesn't depend on the web resource until the potentially blocking task finishes. 在异步过程中,在潜在阻塞任务完成之前,应用程序可以继续执行不依赖于Web资源的其他工作。

Await is specifically designed to deal with something taking time, most typically an I/O request. Await专门用于处理花费时间的事情,最常见的是I / O请求。 Which traditionally was done with a callback when the I/O request was complete. 传统上,当I / O请求完成时,回调就完成了。 Writing code that relies on these callbacks is quite difficult, await greatly simplifies it. 编写依赖于这些回调的代码非常困难,等待大大简化它。 Await just takes care of dealing with the delay, it doesn't otherwise do anything that a thread does. 等待处理延迟,它不执行任何线程所做的事情。 The await expression, what's at the right of the await keyword, is what gets the job done. await表达式,即await关键字右侧的内容,是完成工作的原因。 You can use Async with any method that returns a Task. 您可以对任何返回Task的方法使用Async。 The XxxxAsync() methods are just precooked ones in the .NET framework for common operations that take time. XxxxAsync()方法只是.NET框架中预先生成的方法,用于需要时间的常见操作。 Like downloading data from a web server. 就像从Web服务器下载数据一样。

I would recommend you to read Asynchronous Programming with Async and Await 我建议你阅读Async和Await的异步编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM