The Python Standard Library

1 Concurrent Execution

适当的工具选择取决于要执行的任务(CPU绑定 v.s. IO绑定)和首选样式的开发风格(事件驱动的协作多任务vs抢占式多任务处理)。

1.1 threading — Thread-based parallelism

1.5 queue — A synchronized queue class

该模块实现了三种类型的队列,它们的区别只是检索条目的顺序不同。

  • FIFO队列
  • LIFO队列
  • 优先级队列——条目保持排序(使用heapq模块),首先检索值最低的条目。

1.5.1 Queue Objects

  1. Queue.qsize()
    返回队列的大致大小。注意,qsize() > 0不能保证后续的get()不会阻塞,qsize() < maxsize也不能保证put()不会阻塞。

  2. Queue.empty()
    如果队列为空则返回True,否则返回False。如果empty()返回True,它并不保证后续对put()的调用不会阻塞。类似地,如果empty()返回False,它并不保证后续对get()的调用不会阻塞。

  3. Queue.full()
    如果队列已满则返回True,否则返回False。如果full()返回True,它并不保证后续对get()的调用不会阻塞。类似地,如果full()返回False,它并不保证后续对put()的调用不会阻塞。

  4. Queue.put(item, block=True, timeout=None)
    将item放入队列中。如果可选参数block为true且timeout为None(默认值),则在必要时阻塞,直到有空闲槽位可用。如果timeout是正数,它最多阻塞timeout秒,如果在此时间内没有可用的空闲槽位,则会引发Full异常。否则(block为false),如果有空闲槽位立即可用,则将一个项放到队列中,否则引发Full异常(在这种情况下超时将被忽略)。

  5. Queue.put_nowait(item)
    Equivalent to put(item, False).

  6. Queue.get(block=True, timeout=None)
    从队列中移除并返回一个项。如果可选参数block为true且timeout为None(默认值),则在必要时阻塞,直到有可用的项。如果timeout是正数,它最多阻塞timeout秒,如果在此时间内没有可用项,则引发Empty异常。否则(block为false),返回一个立即可用的项,否则引发Empty异常(在这种情况下超时将被忽略)。

  7. Queue.get_nowait()
    Equivalent to get(False).

提供了两种方法来支持跟踪队列中的任务是否已被守护进程消费者线程完全处理。

  1. Queue.task_done()
    指示以前进入队列的任务已经完成。由队列消费者线程使用。对于用于获取任务的每个get(),后续调用task_done()告诉队列任务的处理已经完成。
    如果join()当前处于阻塞状态,那么当所有的条目都被处理后,它将继续运行(这意味着对于队列中已经put()的每个条目,都收到了task_done()调用)。
    如果调用的次数超过队列中的条目数,则引发ValueError

  2. Queue.join()
    阻塞,直到获得并处理队列中的所有项。
    每当向队列中添加项目时,未完成任务的数量就会增加。每当消费者线程调用task_done()来表示已检索到该条目并完成所有相关工作时,该计数就会下降。当未完成任务的计数下降到0时,join()将解除阻塞。

实现如何等待进入队列的任务完成的例子:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
The Python 3 Standard Library by Example (Developer's Library) by Doug Hellmann English | 12 Jun. 2017 | ASIN: B072QZZDV7 | 1456 Pages | AZW3 | 199.27 MB This is the eBook of the printed book and may not include any media, website access codes, or print supplements that may come packaged with the bound book. Master the Powerful Python 3 Standard Library through Real Code Examples “The genius of Doug’s approach is that with 15 minutes per week, any motivated programmer can learn the Python Standard Library. Doug’s guided tour will help you flip the switch to fully power-up Python’s batteries.” –Raymond Hettinger, Distinguished Python Core Developer The Python 3 Standard Library contains hundreds of modules for interacting with the operating system, interpreter, and Internet–all extensively tested and ready to jump-start application development. Now, Python expert Doug Hellmann introduces every major area of the Python 3.x library through concise source code and output examples. Hellmann’s examples fully demonstrate each feature and are designed for easy learning and reuse. You’ll find practical code for working with text, data structures, algorithms, dates/times, math, the file system, persistence, data exchange, compression, archiving, crypto, processes/threads, networking, Internet capabilities, email, developer and language tools, the runtime, packages, and more. Each section fully covers one module, with links to additional resources, making this book an ideal tutorial and reference. The Python 3 Standard Library by Example introduces Python 3.x’s new libraries, significant functionality changes, and new layout and naming conventions. Hellmann also provides expert porting guidance for moving code from 2.x Python standard library modules to their Python 3.x equivalents. Manipulate text with string, textwrap, re (regular expressions), and difflib Use data structures: enum, collections, array, heapq, queue, struct, copy, and more Implement algorithms elegantly and concisely with functools, itertools, and contextlib Handle dates/times and advanced mathematical tasks Archive and data compression Understand data exchange and persistence, including json, dbm, and sqlite Sign and verify messages cryptographically Manage concurrent operations with processes and threads Test, debug, compile, profile, language, import, and package tools Control interaction at runtime with interpreters or the environment
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淘淘图兔兔呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值