高性能套接字上的异步等待,线程池和多线程(C10k解决方案?) [英] Async-Await vs ThreadPool vs MultiThreading on High-Performance Sockets (C10k Solutions?)
问题描述
我真的对async-await
s,pool
s和thread
s感到困惑.主要问题始于以下问题:当我必须处理10k套接字I/O时该怎么办?" (又名 C10k问题).
I'm really confused about async-await
s, pool
s and thread
s. The main problem starts with this question: "What can I do when I have to handle 10k socket I/O?" (aka The C10k Problem).
- 首先,我尝试使用线程创建自定义池体系结构
使用一个主
Queue
和多个Thread
来处理所有 传入数据.这是一次了解的很棒的经历thread-safety
和multi-threading
,但thread
是过度杀伤 现在是async-await
. - 后来,我用
async-await
实现了一个简单的体系结构,但我 无法理解为什么"async和await关键字不引起 其他要创建的线程." (来自MSDN )?我觉得在那里 必须是某些thread
才能执行>. - 最后,我用
ThreadPool
实现了另一种架构 看起来像我的第一个自定义池.
- First, I tried to make a custom pooling architecture with threads
that uses one main
Queue
and multipleThread
s to process all incoming datas. It was a great experience about understandingthread-safety
andmulti-threading
butthread
is an overkill withasync-await
nowadays. - 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." (from MSDN)? I think there must be somethread
s to do jobs like BackgroundWorker. - Finally, I implemented another architecture with
ThreadPool
and it looks like my first custom pooling.
现在,我认为应该还有其他人对处理 C10k 感到困惑.我的项目是我的游戏项目的专用(中央)服务器,是中心服务器/大堂服务器,例如 MCSG 的大厅或COD的配对服务器.我将执行登录操作,游戏服务器命令执行/查询和信息服务(例如版本,补丁).
Now, I think there should be someone else with me who confused about handling The 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. 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.
(也是,根据服务器的硬件处理1k-10k-100k的连接,但这是一个普遍的问题)
(Also yes, 1k-10k-100k connection handling depending on server hardware but this is a general question)
关键点:在任务并行库之间进行选择和ThreadPool (MSDN博客)
The key point: Choosing Between the Task Parallel Library and the ThreadPool (MSDN Blog)
[ADDITIONAL] 好(基本)的东西,是谁想了解我们在说什么的必读内容:
[ADDITIONAL] Good (basic) things to read who wants to understand what are we talking about:
- Threads
- Async, Await
- ThreadPool
- BackgroundWorker
推荐答案
async
/await
大致类似于为每个线程服务多个客户端,并使用异步I/O和完成通知"方法引用的文章.
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
本身不会引起任何其他线程,但是如果async
方法在线程池上下文中恢复,它们将使用线程池线程.请注意,与ThreadPool
的async
交互是高度优化的;您是否可以使用Thread
或ThreadPool
来获得相同的性能(在合理的开发时间内),这是非常可疑的.
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. Note that the async
interaction with ThreadPool
is highly optimized; it is very doubtful that you can use Thread
or ThreadPool
to get the same performance (with a reasonable time for development).
如果可以的话,我建议您使用现有的协议-例如SignalR.这将大大简化您的代码,因为编写自己的TCP/IP协议有很多(很多)陷阱. SignalR可以是自托管的,也可以托管在ASP.NET上.
If you can, I'd recommend using an existing protocol - e.g., SignalR. This will greatly simplify your code, since there are many (many) pitfalls to writing your own TCP/IP protocol. SignalR can be self-hosted or hosted on ASP.NET.
这篇关于高性能套接字上的异步等待,线程池和多线程(C10k解决方案?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!