来自 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()
做了两件事
- 使用
requests
库抓取 url - 使用
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/