简体   繁体   English

受CPU约束的线程是否总是需要Python中的GIL?

[英]Does a cpu-bound thread always reaquire the GIL in Python?

I was going through David Beazley's GIL-related slides, in an attempt to understand what causes threading switching in Python. 我正在浏览David Beazley的GIL相关幻灯片,试图了解是什么导致Python中的线程切换。 However, a lot of it is over my head. 但是,很多事情困扰着我。

http://www.dabeaz.com/python/UnderstandingGIL.pdf http://www.dabeaz.com/python/UnderstandingGIL.pdf

It seems that after 100 ticks or so, the current thread releases the GIL, and will reacquire it if it's CPU Bound? 似乎经过100个滴答之后,当前线程释放了GIL,并且如果它是CPU绑定的,它将重新获取它?

On the other hand if it sleeps or is I/O Bound, then another thread will acquire the GIL. 另一方面,如果它休眠或处于I / O绑定状态,则另一个线程将获取GIL。

So if a thread is CPU bound it will keep reacquiring until it finished the task? 因此,如果某个线程受CPU限制,它将继续获取直到完成任务为止?

It will try to reacquire the GIL until it is done, or is paused at I/O or similar. 它将尝试重新获取GIL,直到完成或在I / O处暂停或类似操作。 However, if there are other CPU bound in the program, they will also try to acquire the GIL, so whenever the GIL is released, an arbitration is needed, to decide who will be allowed to get it. 但是,如果程序中还有其他CPU绑定,它们还将尝试获取GIL,因此无论何时释放GIL,都需要进行仲裁,以决定允许谁获取它。

But if there are no other threads ready to run (eg all others are waiting for I/O) then the running thread will get the lock everyt time. 但是,如果没有其他线程可以运行(例如,所有其他线程都在等待I / O),则正在运行的线程将每次都获得锁。 But then there will still be an overhead every 100 ticks for the release-reacquire dance. 但是,每100个滴答声仍然会有开销,需要释放释放的舞蹈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM