Event driven server that wastes CPU when threaded doesn't

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Snor

    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?

  • Fredrik Lundh

    #2
    Re: Event driven server that wastes CPU when threaded doesn't

    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>

    Comment

    • Bjoern Schliessmann

      #3
      Re: Event driven server that wastes CPU when threaded doesn't

      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

      Comment

      • Nick Vatamaniuc

        #4
        Re: Event driven server that wastes CPU when threaded doesn't

        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_fini shed
        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_fini shed 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?:


        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?

        Comment

        • Carl Banks

          #5
          Re: Event driven server that wastes CPU when threaded doesn't

          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

          Comment

          • Paul Rubin

            #6
            Re: Event driven server that wastes CPU when threaded doesn't

            "Nick Vatamaniuc" <vatamane@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?

            Comment

            • Felipe Almeida Lessa

              #7
              Re: Event driven server that wastes CPU when threaded doesn't

              29 Oct 2006 14:18:02 -0800, Paul Rubin <"http://phr.cx"@nospam. invalid>:
              "Nick Vatamaniuc" <vatamane@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:





              --
              Felipe.

              Comment

              • Nick Vatamaniuc

                #8
                Re: Event driven server that wastes CPU when threaded doesn't

                Try the --skip-networking option for mysqld

                Paul Rubin wrote:
                "Nick Vatamaniuc" <vatamane@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?

                Comment

                • Nick Vatamaniuc

                  #9
                  Re: Event driven server that wastes CPU when threaded doesn't

                  Good point. enterprise.adba pi 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 <vatamane@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_fini shed
                  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_fini shed 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: DeprecationWarn ing: Create your own event dispatching mechanism, twisted.python. dispatch will soon be no more.
                  >
                  Take a look at <http://twistedmatrix.c om/documents/current/api/twisted.enterpr ise.adbapi.html >.
                  >
                  Jean-Paul

                  Comment

                  • Bjoern Schliessmann

                    #10
                    Re: Event driven server that wastes CPU when threaded doesn't

                    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

                    Comment

                    • Fredrik Lundh

                      #11
                      Re: Event driven server that wastes CPU when threaded doesn't

                      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>

                      Comment

                      • Bryan Olson

                        #12
                        Re: Event driven server that wastes CPU when threaded doesn't

                        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

                        Comment

                        • Bjoern Schliessmann

                          #13
                          Re: Event driven server that wastes CPU when threaded doesn't

                          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

                          Comment

                          • Magnus Lycka

                            #14
                            Re: Event driven server that wastes CPU when threaded doesn't

                            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


                            "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."

                            Comment

                            • Snor

                              #15
                              Re: Event driven server that wastes CPU when threaded doesn't

                              You got a lot of long-winded replies, but the short reply is that
                              Twisted has packaged solutions for this. Seehttp://twistedmatrix.c om/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 :)

                              Comment

                              Working...