[英]Understanding python GIL - I/O bound vs CPU bound
From python threading documentation 来自python线程文档
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).
在CPython中,由于使用了全局解释器锁,因此只有一个线程可以一次执行Python代码(即使某些面向性能的库可能克服了此限制)。 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.
但是,如果您要同时运行多个I / O绑定任务,则线程化仍然是合适的模型。
Now I have a thread worker like this 现在我有一个这样的线程工作者
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
Here w.analyze()
doing two things 在这里
w.analyze()
做两件事
requests
library requests
库抓取网址 pyv8
javascript library pyv8
JavaScript库分析pyv8
html As far as I know, 1
is I/O bound and 2
is CPU bound. 据我所知,
1
是I / O绑定, 2
是CPU绑定。
Does that mean, GIL applied for 2
and my program won't work properly? 这是否意味着GIL申请了
2
,但我的程序无法正常运行?
The GIL
description does not say anything about correctness, only about efficiency. GIL
描述没有GIL
正确性,仅涉及效率。
If 2
is CPU bound, you will not be able to get multicore performance out of threading, but your program will still perform correctly . 如果
2
受CPU限制,您将无法从线程中获得多核性能,但您的程序仍将正确执行。
If you care about CPU Parallelism, you should use Python's multiprocessing
library. 如果您关心CPU并行性,则应该使用Python的
multiprocessing
库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.