why the method get() of python Queue is hang on there?

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

    why the method get() of python Queue is hang on there?

    Hi,
    I am using Queue from python2.4. Here is what happen to me:

    import Queue
    b = Queue.Queue(0)
    b.put(9999)
    b.get() # this is ok, it pops out 9999
    b.get() # this one does not return anything and is hang on there

    Anybody knows what is going on with the second b.get()?

    ouyang

  • skip@pobox.com

    #2
    Re: why the method get() of python Queue is hang on there?

    zxoimport Queue
    zxob = Queue.Queue(0)
    zxob.put(9999)
    zxob.get() # this is ok, it pops out 9999
    zxob.get() # this one does not return anything and is hang on there

    zxoAnybody knows what is going on with the second b.get()?

    Queue objects are meant to be used in a multithreaded application. By
    default, when the Queue is empty, a consumer calling get() will block until
    a producer put()s something else into it. From the documentation:

    get([block[, timeout]])
    Remove and return an item from the queue. If optional args block is
    true and timeout is None (the default), block if necessary until an
    item is available....

    Skip

    Comment

    • Tim Chase

      #3
      Re: why the method get() of python Queue is hang on there?

      import Queue
      b = Queue.Queue(0)
      b.put(9999)
      b.get() # this is ok, it pops out 9999
      b.get() # this one does not return anything and is hang on
      there
      >
      Anybody knows what is going on with the second b.get()?
      >>help(Queue.Qu eue)
      :
      :
      | get(self, block=True, timeout=None)
      | Remove and return an item from the queue.
      |
      | If optional args 'block' is true and
      | 'timeout' is None (the default), block if
      | necessary until an item is available. If
      | 'timeout' is a positive number, it blocks
      | at most 'timeout' seconds and raises the
      | Empty exception if no item was available
      | within that time. Otherwise ('block' is
      | false), return an item if one is
      | immediately available, else raise the
      | Empty exception ('timeout' is ignored in
      | that case).
      |
      | get_nowait(self )
      | Remove and return an item from the queue
      | without blocking.
      |
      | Only get an item if one is immediately
      | available. Otherwise raise the Empty
      | exception.
      :
      :

      Notice that by default, get() will block until
      there's something to read. You can use either

      b.get(block=Fal se)

      or

      b.get_nowait()

      In either case, an exception will be raised when
      the Queue is empty, to let you know as much. Just
      trap for the exception and do as you please.

      -tkc




      Comment

      • Fredrik Lundh

        #4
        Re: why the method get() of python Queue is hang on there?

        zxo102 wrote:
        Hi,
        I am using Queue from python2.4. Here is what happen to me:
        >
        import Queue
        b = Queue.Queue(0)
        b.put(9999)
        b.get() # this is ok, it pops out 9999
        b.get() # this one does not return anything and is hang on there
        >
        Anybody knows what is going on with the second b.get()?
        the documentation has the answer:

        get( [block[, timeout]])

        Remove and return an item from the queue. If optional args block
        is true and timeout is None (the default), block if necessary until
        an item is available. /.../



        </F>

        Comment

        • Eric Brunel

          #5
          Re: why the method get() of python Queue is hang on there?

          On Mon, 14 Aug 2006 17:10:13 +0200, zxo102 <zxo102@gmail.c omwrote:
          Hi,
          I am using Queue from python2.4. Here is what happen to me:
          >
          import Queue
          b = Queue.Queue(0)
          b.put(9999)
          b.get() # this is ok, it pops out 9999
          b.get() # this one does not return anything and is hang on there
          >
          Anybody knows what is going on with the second b.get()?
          What did you expect? Since you've done only one put in the Queue, there's
          nothing left in it. Since queues are meant to communicate between threads
          - and also often to synchronize them -, the default behaviour for the get
          when the queue is empty is to wait for something to be put in it. If you
          want your second get to fail, use the get_nowait method.

          HTH
          --
          python -c "print ''.join([chr(154 - ord(c)) for c in
          'U(17zX(%,5.zmz 5(17l8(%,5.Z*(9 3-965$l7+-'])"

          Comment

          • zxo102

            #6
            Re: why the method get() of python Queue is hang on there?

            Thanks for your guys. I got it. I thought Queue can be used anywhere
            in the code and the second b.get() would return a "None".

            Ouyang


            zxo102 写道:
            Hi,
            I am using Queue from python2.4. Here is what happen to me:
            >
            import Queue
            b = Queue.Queue(0)
            b.put(9999)
            b.get() # this is ok, it pops out 9999
            b.get() # this one does not return anything and is hang on there

            Anybody knows what is going on with the second b.get()?

            ouyang

            Comment

            • Gabriel Genellina

              #7
              Re: why the method get() of python Queue is hang on there?

              At Monday 14/8/2006 12:35, zxo102 wrote:
              >Thanks for your guys. I got it. I thought Queue can be used anywhere
              >in the code and the second b.get() would return a "None".
              You can use a list as a generic queue, with append (== push) and pop(0)



              Gabriel Genellina
              Softlab SRL





              _______________ _______________ _______________ _____
              Preguntá. Respondé. Descubrí.
              Todo lo que querías saber, y lo que ni imaginabas,
              está en Yahoo! Respuestas (Beta).
              ¡Probalo ya!


              Comment

              Working...