• 学习python的queue


      The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the threading module.(也就是说Queue是线程安全的)

      The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO (普通队列)queue, the first tasks added are the first retrieved. In a LIFO (栈) queue, the most recently added entry is the first retrieved (operating like a stack). With a PriorityQueue(优先级队列), the entries are kept sorted (using the heapq module) and the lowest valued entry is retrieved first.

      下面说说三个重要的函数:

      Queue.get([block[timeout]])

      Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block(其它线程无法操作queue) 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).

      Queue.put(item[block[timeout]])

      Put item into the queue. If optional args block is true and timeout is None (the default), block(其它线程无法操作queue) if necessary until a free slot is available(一直到这个item在queue可用). If timeout is a positive number, it blocks at mosttimeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).

      Queue.task_done()

      Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.(每次get()函数就会产生一个task,调用task_done()函数告诉队列task 处理完毕)

      If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

      Raises a ValueError if called more times than there were items placed in the queue.

      Queue.join()

      Blocks(这里指的是当前线程) until all items in the queue have been gotten and processed(task_done()).

      The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

  • 相关阅读:
    ajax全套
    url设计规范
    内置下划线方法
    rest_framework视图
    rest_framework
    数据库设置
    HDU 6231
    HDU 6242
    CodeForces 546D
    CodeForces 940E
  • 原文地址:https://www.cnblogs.com/slider/p/2557499.html
Copyright © 2020-2023  润新知