By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,002 Members | 2,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,002 IT Pros & Developers. It's quick & easy.

Event driven server that wastes CPU when threaded doesn't

P: n/a
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
Share this Question
Share on Google+
14 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.