事件驱动的服务器在线程化时不会浪费CPU

编程入门 行业动态 更新时间:2024-10-26 08:31:44
本文介绍了事件驱动的服务器在线程化时不会浪费CPU的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建一个大厅和用于多人游戏的游戏服务器, 并且在服务器设计的早期遇到了问题。在使用线程服务器和使用事件驱动的服务器之间我被卡住了。我已经一次又一次地告诉我b $ b我应该使用一个事件驱动的服务器 设计(即使用扭曲)。 客户之间有很多互动,他们经常会需要写入相同的价值清单,这当然会成为一个问题。\\ b 问题使用线程服务器 - 所以事件驱动解决了这个问题, 我认为它可以解决我所有的问题。但是,客户端的一些请求需要服务器继续查询mySQL 服务器(与服务器分开的机器)。当发生这种情况时,与mySQL服务器的通信需要 ,并且可能有另外100个客户端等待发出请求,因此存在少量滞后。此时,,同时服务器在等待来自mySQL服务器的 响应时空转 - 显然不是一个好的服务器型号。 我希望服务器能够在任何给定机器上支持尽可能多的用户 - 因此浪费的CPU周期是我试图避免的b $ b b。 唯一的解决方案是使用线程服务器让我的客户在他们的请求中做出并在最快的时间内收到响应?

I''m attempting to create a lobby & game server for a multiplayer game, and have hit a problem early on with the server design. I am stuck between using a threaded server, and using an event driven server. I''ve been told time and time again that I should use an event driven server design (that is, use twisted). There is a lot of interaction between the clients and they would often need to write to the same list of values, which of course becomes a problem with a threaded server - so event driven solves that problem, and I assumed it would solve all my problems. However some requests from clients would require that the server goes on to query a mySQL server (separate machine from the server). As this occurs, there is a small amount of lag while the communication with the mySQL server takes place, and there could be another 100 clients waiting to make a request at this point, meanwhile the server is idling while waiting for a response from the mySQL server - obviously not a good server model. I will want the server to support as many users as is possible on any given machine - and so wasted CPU cycles is something I am trying to avoid. Is the only solution to use a threaded server to let my clients make their requests and receive a response in the fastest possible time?

推荐答案

Snor写道: Snor wrote: 是唯一可以使用的解决方案一个线程服务器让我的客户在他们的请求中做出并在最快的时间内收到响应? Is the only solution to use a threaded server to let my clients make their requests and receive a response in the fastest possible time?

因为问题是你需要等待数据库,也许 你可以使用一个或多个线程来处理数据库,并保持 使用事件与客户交谈? < / F>

since the problem is that you need to wait for database anyway, maybe you could use one or more threads to deal with the database, and keep using events to talk to the clients? </F>

Snor写道: Snor wrote: 客户之间有很多互动,他们会经常需要写入b $ b相同的值列表,当然 成为线程服务器的问题 - 所以事件驱动解决了这个问题,我认为它会解决我所有的问题。 There is a lot of interaction between the clients and they would often need to write to the same list of values, which of course becomes a problem with a threaded server - so event driven solves that problem, and I assumed it would solve all my problems.

哪个问题,以及为什么当然?对不起,我不能跟着你 这里:)

Which problem, and why "of course"? Sorry, I can''t follow you here :)

我希望服务器支持尽可能多的用户 任何给定的机器 - 因此浪费的CPU周期是我想要避免的b $ b。 I will want the server to support as many users as is possible on any given machine - and so wasted CPU cycles is something I am trying to avoid.

我不确定你是如何连接到那个SQL服务器的......你好b / b 不应该等待响应MySQL服务器阻塞 的方式,但要么使用协议的dataReceived()方法 实例,要么使用延迟,如果这是不可能的话实例 在答案可用时触发。这也可以用你的客户连接 。 问候, Bj? - BOFH借口#354: 口香糖/ dev / sd3c

I''m not exactly sure how you connect to that SQL server ... you shouldn''t wait for the response of the MySQL server in a blocking way, but either using dataReceived() method of the protocol instance or, if that isn''t possible, by using a Deferred instance that fires when the answer is available. This is also possible with your client connections. Regards, Bj?rn -- BOFH excuse #354: Chewing gum on /dev/sd3c

Snor, 最简单的解决方案是更换系统并将数据库放在 相同的机器上,从而大大缩短时间每个数据库查询需要完成(完全避免TCP堆栈)。通过这种方式你可能不会改变你的应用程序逻辑。 如果这不是一个选项,那么你将面临一个问题 将线程编程模型与基于事件的模型连接 (扭曲等等)。所以你的工作就是将两者联系起来。在其他 单词中,基于事件的模型将线程数据库访问视为基于事件 。并且DB驱动程序将基于事件的系统视为线程化。所以你的事件调度员你可以添加像db_request_finished这样的事件 然后当你向DB请求连接时,回调将是 提供。连接将在其自己的线程中进行,然后当它完成时,它会将db_request_finished和相应的 回调函数放在事件队列中。我不知道怎么把 整合到Twisted事件调度员中......也许这个类是 答案?: twistedmatrix/documents/c...ispatcher。 html 希望这会有所帮助, Nick V. Snor写道: Snor, The simplest solution is to change your system and put the DB on the same machine thus greatly reducing the time it takes for each DB query to complete (avoid the TCP stack completely). This way you might not have to change your application logic. If that is not an option, then you are faced with a problem of connecting a threaded programming model with an event based model (twisted and and such). So your job is to interface the two. In other words make the event based model see the threaded DB access as event based. And the DB driver to see the event-based system as threaded. So in your event dispatcher you could add events like db_request_finished then when a connection is requested to the DB, a callback will be supplied. The connection will take place in its own thread, then when it is finished it will put the db_request_finished and the respective callback function on the event queue. I am not sure how to integrate that into the Twisted event dispatcher... perhaps this class is the answer?: twistedmatrix/documents/c...ispatcher.html Hope this helps, Nick V. Snor wrote: 我正在尝试创建一个大厅&用于多人游戏的游戏服务器, 并且在服务器设计的早期遇到了问题。在使用线程服务器和使用事件驱动的服务器之间我被卡住了。我已经一次又一次地告诉我b $ b我应该使用一个事件驱动的服务器 设计(即使用扭曲)。 客户之间有很多互动,他们经常会需要写入相同的价值清单,这当然会成为一个问题。\\ b 问题使用线程服务器 - 所以事件驱动解决了这个问题, 我认为它可以解决我所有的问题。但是,客户端的一些请求需要服务器继续查询mySQL 服务器(与服务器分开的机器)。当发生这种情况时,与mySQL服务器的通信需要 ,并且可能有另外100个客户端等待发出请求,因此存在少量滞后。此时,,同时服务器在等待来自mySQL服务器的 响应时空转 - 显然不是一个好的服务器型号。 我希望服务器能够在任何给定机器上支持尽可能多的用户 - 因此浪费的CPU周期是我试图避免的b $ b b。 唯一的解决方案是使用线程服务器让我的客户在他们的请求中做出并在最快的时间内收到响应? I''m attempting to create a lobby & game server for a multiplayer game, and have hit a problem early on with the server design. I am stuck between using a threaded server, and using an event driven server. I''ve been told time and time again that I should use an event driven server design (that is, use twisted). There is a lot of interaction between the clients and they would often need to write to the same list of values, which of course becomes a problem with a threaded server - so event driven solves that problem, and I assumed it would solve all my problems. However some requests from clients would require that the server goes on to query a mySQL server (separate machine from the server). As this occurs, there is a small amount of lag while the communication with the mySQL server takes place, and there could be another 100 clients waiting to make a request at this point, meanwhile the server is idling while waiting for a response from the mySQL server - obviously not a good server model. I will want the server to support as many users as is possible on any given machine - and so wasted CPU cycles is something I am trying to avoid. Is the only solution to use a threaded server to let my clients make their requests and receive a response in the fastest possible time?

更多推荐

事件驱动的服务器在线程化时不会浪费CPU

本文发布于:2023-11-23 10:35:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   事件   服务器   CPU

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!