473,396 Members | 1,756 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Event driven server that wastes CPU when threaded doesn't

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?

Oct 29 '06 #1
14 2085
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?
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>

Oct 29 '06 #2
Snor wrote:
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 :)
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.
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

Oct 29 '06 #3
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?:
http://twistedmatrix.com/documents/c...ispatcher.html

Hope this helps,
Nick V.

Snor wrote:
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?
Oct 29 '06 #4
Snor wrote:
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.
Isn't it possible to use asynchronous communication to talk to the SQL
server as well?

I don't know a lot about Twisted, but I'd think you could create some
sort of connection with the SQL server and have TwistedMatrix manage
and react to the SQL server events the same as it does with your
clients. Being that SQL is so common, I bet someone's already written
higher-level protocol handlers.
Carl Banks

Oct 29 '06 #5
"Nick Vatamaniuc" <va******@gmail.comwrites:
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).
Since when do any db's let you avoid the TCP stack, even on the same
machine?
Oct 29 '06 #6
29 Oct 2006 14:18:02 -0800, Paul Rubin <"http://phr.cx"@nospam.invalid>:
"Nick Vatamaniuc" <va******@gmail.comwrites:
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).

Since when do any db's let you avoid the TCP stack, even on the same
machine?
Since there are Unix sockets? A quick google:

http://dev.mysql.com/doc/refman/5.0/...x-servers.html
http://archives.postgresql.org/pgsql...0/msg00568.php
--
Felipe.
Oct 29 '06 #7
Try the --skip-networking option for mysqld

Paul Rubin wrote:
"Nick Vatamaniuc" <va******@gmail.comwrites:
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).

Since when do any db's let you avoid the TCP stack, even on the same
machine?
Oct 29 '06 #8
Good point. enterprise.adbapi is designed to solve the problem. The
other interface was deprecated.

Thanks,
Nick Vatamaniuc
Jean-Paul Calderone wrote:
On 29 Oct 2006 13:13:32 -0800, Nick Vatamaniuc <va******@gmail.comwrote:
Snor wrote:
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,

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?:
http://twistedmatrix.com/documents/c...ispatcher.html

Note, however:
>from twisted.python import dispatch
__main__:1: DeprecationWarning: Create your own event dispatching mechanism, twisted.python.dispatch will soon be no more.

Take a look at <http://twistedmatrix.com/documents/current/api/twisted.enterprise.adbapi.html>.

Jean-Paul
Oct 29 '06 #9
Nick Vatamaniuc wrote:
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).
I don't really see how threads could avoid a problem with delays in
one connection ...

Regards,
Björn

--
BOFH excuse #175:

OS swapped to disk

Oct 30 '06 #10
Bjoern Schliessmann wrote:
>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).

I don't really see how threads could avoid a problem with delays in
one connection ...
by running the database queries in one or more separate threads, you can
still serve requests that don't hit the database (either because they're
entirely self-contained, or because they only rely on cached data).

</F>

Oct 30 '06 #11
Snor wrote:
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).
I didn't hear the specifics of how you got that advice, so I
can't comment specifically. I will say that I've have heard a
lot of advice against threads that struck me as simply naive.
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. [...]
The purely event-driven style solves some problems, but creates
others. You're forced to structure your code around the blocking
behavior, and that's not the structure anyone would choose for
clarity or maintainability.

Suppose we're enhancing an event-driven system, and decide to
relocate some datum from a variable to the database. Suppose in
one or more places, accessing the data happens in a call chain
several function down from the event loop. We can't just change
the function that accesses the data because a synchronous
database call could block and stop event processing. Every
interface on the call chain is broken.
[...]
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.
Python is a great scripting language, but squeezing out machine
performance is not where scripting languages shine. That said, you
might start in Python, to see if your system is successful and the
performance of your server really is a limiting factor.

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?
Maybe. Probably not. It's all good -- multi-threading is your friend.
--
--Bryan
Oct 31 '06 #12
Fredrik Lundh wrote:
by running the database queries in one or more separate threads,
you can still serve requests that don't hit the database (either
because they're entirely self-contained, or because they only rely
on cached data).
Nothing that couldn't also be solved without threads. :)

Regards,
Björn

--
BOFH excuse #362:

Plasma conduit breach

Oct 31 '06 #13
Snor wrote:
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).
[snip]
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?
You got a lot of long-winded replies, but the short reply is that
Twisted has packaged solutions for this. See
http://twistedmatrix.com/projects/core/enterprise

"Twisted provides an interface to any Python DB-API 2.0 compliant
database through an asynchronous interface which allows database
connections to be used, and multiplexed by multiple threads, while still
remaining thread-safe for use with Twisted's event-based main loop.
Twisted Enterprise provides these services by leveraging Twisted
Internet's utilities for managing thread pools and asynchronous
programming."
Nov 2 '06 #14
You got a lot of long-winded replies, but the short reply is that
Twisted has packaged solutions for this. Seehttp://twistedmatrix.com/projects/core/enterprise

"Twisted provides an interface to any Python DB-API 2.0 compliant
database through an asynchronous interface which allows database
connections to be used, and multiplexed by multiple threads, while still
remaining thread-safe for use with Twisted's event-based main loop.
Twisted Enterprise provides these services by leveraging Twisted
Internet's utilities for managing thread pools and asynchronous
programming."
Thanks for this - exactly what I needed. Thanks for the other replies
too even if they weren't so much help :)

Nov 5 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: alanrn | last post by:
For all you seasoned VB programmers this is going to be a no brainer. However, as a C programmer learning VB I'm having trouble getting my arms around the event-driven nature of VB. Suppose I...
6
by: Amir Hardon | last post by:
I am dynamically adding rows to a table, and each row have a button which removes it. I have successfully implemented this for mozilla but I'm having troubles with IE, here is how I did it: ...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
5
by: maurban | last post by:
Hi there experts, I have a gridview with a couple textboxes and a dropdownlist. I'm trying to insert a default value into my database driven dropdownlist. I'm doing this in the rowdatabound...
4
by: AzizMandar | last post by:
C++ Event Coding Questions I have done some simple programs in C++ and read a lot of good C++ books (Including The C++ Programing Language, and C++ Primer) I am trying to understand and...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
0
MMcCarthy
by: MMcCarthy | last post by:
VBA is described as an Event driven programming language. What is meant by this? Access, like most Windows programs, is an event driven application. This means that nothing happens unless it is...
10
by: hollex2108 | last post by:
Hi, I search for a method how my function detect an event while js is carry out a loop. I think it's not really important for a problem solving, but goodness knows: I want to automate a...
8
by: Brad Walton | last post by:
Hello. First post, but been doing a bit of reading here. I am working on a project in Java, but decided to switch over to C# after seeing some of the additional features I can get from C#. One of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.