global interpreter lock not working as it should
brueckd at tbye.com
brueckd at tbye.com
Tue Jul 30 15:39:16 EDT 2002
On Tue, 30 Jul 2002, anton wilson wrote:
> On Tuesday 30 July 2002 04:55 am, Martin v. Löwis wrote:
> I would disagree.
> In the Python documentation it states:
>
> "In order to support multi-threaded Python programs, the
> interpreter regularly releases and reacquires the lock -- by default, every
> ten bytecode instructions "
>
> What's the purpose of releasing an reaquiring the lock if no other threads
> can run?
Please consider that, because there's so many multithreaded Python
programs that work quite well, it's rather unlikely that the threading
implementation is outright broken. *Maybe* some improvement needs to be
made, but from your posts it sounds more like you don't understand how
things work at the C level, much less in Python. When the lock is released
at the end of its regular interval, an *attempt* is made to reacquire it
immediately, but there's no guarantee that the current thread will get it
right away (and if another thread is already blocking on an attempt to get
the lock then the other one will probably "win" most of the time anyway).
> but the main problem is that the GIL is NOT implemented properly with
> pthreads.
That's an interesting perspective, except that there's a ton of
multithreaded Python programs that work just fine. Granted, it is very
common for the GIL to change hands due to blocking calls (I/O, sleep,
etc.), but the put-it-up-for-grabs-every-10-instructions functionality
works just fine too. Consider:
import threading, time
COUNT = 3
counters = [0] * COUNT
def Worker(i):
while 1:
counters[i] += 1
for i in range(COUNT):
threading.Thread(target=Worker, args=(i,)).start()
while 1:
time.sleep(1.0)
print counters
Here's some output:
[162565, 176016, 165796]
[329009, 327856, 333183]
[497881, 496857, 498133]
[665567, 679094, 643678]
[810255, 845521, 811988]
[968056, 1008142, 974790]
Lo and behold, each thread is getting execution time, and nearly equal
execution time at that!
-Dave
More information about the Python-list
mailing list