SocketServer and timers

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

    #1

    SocketServer and timers

    Hi,

    I have one thread app using SocketServer and use server_forever( ) as a
    main loop. All works fine, but now I need certain timer checking let's
    say every 1 second something and stopping the main loop. So questions are:
    -how to stop serve_forever
    -how to implement timers

    thx, alf
  • bryanjugglercryptographer@yahoo.com

    #2
    Re: SocketServer and timers


    alf wrote:
    I have one thread app using SocketServer and use server_forever( ) as a
    main loop. All works fine, but now I need certain timer checking let's
    say every 1 second something and stopping the main loop. So questions are:
    -how to stop serve_forever
    Override serve_forever() and replace "while 1:" with something like
    "while self.continue_f lag:".
    -how to implement timers
    You could override get_request(), and use select.select() to wait for
    either the socket to become readable (so the accept() won't block), or
    a timeout to expire. If you are not already, use the threading or
    forking
    mixin so that the request handler won't stop everthing if it blocks.


    --
    --Bryan

    Comment

    • Simon Forman

      #3
      Re: SocketServer and timers

      alf wrote:
      Hi,
      >
      I have one thread app using SocketServer and use server_forever( ) as a
      main loop. All works fine, but now I need certain timer checking let's
      say every 1 second something and stopping the main loop. So questions are:
      -how to stop serve_forever
      -how to implement timers
      >
      thx, alf
      Do you really need a timer, or do you just want to check something
      every second or so?

      If the latter, then something like this would do it:

      from time import time

      INTERVAL = 1.0

      RUN = True

      while RUN:

      # Get a time in the future.
      T = time() + INTERVAL

      # Serve requests until then.
      while time() < T:
      server.handle_r equest()

      # Check whatever.
      if check():
      # Do something, for example, stop running.
      RUN = False

      HTH,
      ~Simon

      Comment

      • bryanjugglercryptographer@yahoo.com

        #4
        Re: SocketServer and timers


        Simon Forman wrote:
        alf wrote:
        Hi,

        I have one thread app using SocketServer and use server_forever( ) as a
        main loop. All works fine, but now I need certain timer checking let's
        say every 1 second something and stopping the main loop. So questions are:
        -how to stop serve_forever
        -how to implement timers

        thx, alf
        >
        Do you really need a timer, or do you just want to check something
        every second or so?
        >
        If the latter, then something like this would do it:
        >
        from time import time
        >
        INTERVAL = 1.0
        >
        RUN = True
        >
        while RUN:
        >
        # Get a time in the future.
        T = time() + INTERVAL
        >
        # Serve requests until then.
        while time() < T:
        server.handle_r equest()
        # Check whatever.
        if check():
        # Do something, for example, stop running.
        RUN = False
        That alone does not work. If server.handle_r equest() blocks,
        you don't get to the check(). You need some kind of timeout
        in handle_request( ).


        --
        --Bryan

        Comment

        • Simon Forman

          #5
          Re: SocketServer and timers

          bryanjugglercry ptographer@yaho o.com wrote:
          ....
          >
          That alone does not work. If server.handle_r equest() blocks,
          you don't get to the check(). You need some kind of timeout
          in handle_request( ).
          >
          >
          --
          --Bryan
          Ach! You're right. I didn't consider that handle_request( ) might
          block..

          Comment

          • alf

            #6
            Re: SocketServer and timers

            bryanjugglercry ptographer@yaho o.com wrote:
            alf wrote:
            >
            >>I have one thread app using SocketServer and use server_forever( ) as a
            >>main loop. All works fine, but now I need certain timer checking let's
            >>say every 1 second something and stopping the main loop. So questions are:
            >> -how to stop serve_forever
            >
            >
            Override serve_forever() and replace "while 1:" with something like
            "while self.continue_f lag:".
            >
            >
            >> -how to implement timers
            >
            >
            You could override get_request(), and use select.select() to wait for
            either the socket to become readable (so the accept() won't block), or
            a timeout to expire. If you are not already, use the threading or
            forking
            mixin so that the request handler won't stop everthing if it blocks.
            >
            >
            thx for a suggestion, will try it out ...

            Comment

            Working...