python - 了解 python GIL - I/O 绑定(bind)与 CPU 绑定(bind)

标签 python multithreading gil

来自 python threading文档

In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation). If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing. However, threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously.

现在我有一个这样的线程 worker

def worker(queue):
    queue_full = True
    while queue_full:
        try:
            url = queue.get(False)
            w = Wappalyzer(url)
            w.analyze()
            queue.task_done()

        except Queue.Empty:
            queue_full = False

这里 w.analyze() 做了两件事

  1. 使用 requests 库抓取 url
  2. 使用 pyv8 javascript 库分析抓取的 html

据我所知,1 受 I/O 限制,2 受 CPU 限制。

这是否意味着,GIL 申请了 2 而我的程序将无法正常运行?

最佳答案

GIL 的描述并没有说明正确性,只说明了效率。

如果 2 受 CPU 限制,您将无法从线程中获得多核性能,但您的程序仍将正确执行。

如果您关心 CPU 并行性,您应该使用 Python 的 multiprocessing 库。

关于python - 了解 python GIL - I/O 绑定(bind)与 CPU 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574367/

相关文章:

c# - 在 Task 中等待后代码的执行上下文

python - 选择要从 $lookup 返回的字段

java - 如何控制Apache Tomcat开启的线程数

python - 查找符号的小写(移位)形式

c# - 从线程访问和更改 winform webbrowser

python - 尽管有 GIL,多线程仍可加速 CPU 密集型任务

python-3.x - python3 为什么/如何按创建顺序安排线程?

python - 一个线程安全的 memoize 装饰器

python - 如何在 Python 2.6 CentOS 中安装 MySQLdb

python - 如何在Python中从字符串中提取子字符串?