简体   繁体   中英

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. However, a lot of it is over my head.

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?

On the other hand if it sleeps or is I/O Bound, then another thread will acquire the GIL.

So if a thread is CPU bound it will keep reacquiring until it finished the task?

It will try to reacquire the GIL until it is done, or is paused at I/O or similar. 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.

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. But then there will still be an overhead every 100 ticks for the release-reacquire dance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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