Is global thread safety counter in python?

import threading
import time


counter = 0

def increase(name):
    global counter
    i = 0
    while i < 30:
        # this for loop is for consuming cpu
        for x in xrange(100000):
            1+1
        counter += 1
        print name + " " + str(counter)
        i += 1


if __name__ == '__main__':
    threads = []
    try:
        for i in xrange(100):
           name = "Thread-" + str(i)
           t = threading.Thread( target=increase, args=(name,) )
           t.start()
           threads.append(t)
    except:
          print "Error: unable to start thread"

    for t in threads:
        t.join()

Python version is 2.7.5.

In the above code, I run it several times, the final result is always 3000.

And this code is also an example of this blog. http://effbot.org/zone/thread-synchronization.htm

But this blog also mentions that:

In general, this approach only works if the shared resource consists of one instance of the main data type, such as a string variable, number, list, or dictionary. Here are some thread safe operations:

  • reading or replacing a single instance attribute
  • reading or replacing one global variable
  • select an item from a list
  • change the list in place (for example, adding an item by adding)
  • select an item from a dictionary
  • (, clear)

, , python?

1

Linux- CentOS Linux release 7.2.1511, 3.10.0-123.el7.x86_64 #1 SMP Mon Jun 30 12:09:22 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux.

mac - 10.11.5 (15F34), python - 2.7.10.

Mac, , , - .

Linux, .

counter:3000, expected:3000
counter:3000, expected:3000
counter:3000, expected:3000
counter:3000, expected:3000
counter:3000, expected:3000

- , ?

2

, ​​Linux, , . linux-, 4 , .

Python GIL, , , , . GIL ?

, ?

.

+4
3

, CPython. GIL , += :

Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import dis
>>> counter = 0
>>> def inc():
...     global counter
...     counter += 1
... 
>>> dis.dis(inc)
  3           0 LOAD_GLOBAL              0 (counter)
              3 LOAD_CONST               1 (1)
              6 INPLACE_ADD         
              7 STORE_GLOBAL             0 (counter)
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE        

counter , ; , LOAD_GLOBAL STORE_GLOBAL . , , inc, :

Thread 1                Thread 2
LOAD_GLOBAL 0
LOAD_CONST 1
INPLACE_ADD
                        LOAD_GLOBAL 0
                        LOAD_CONST 1
                        INPLACE_ADD
                        STORE_GLOBAL 0
STORE_GLOBAL 0
LOAD_CONST 0
RETURN_VALUE
                        LOAD_CONST 0
                        RETURN_VALUE

, 2, , 1 counter .

, "":

import threading
import time

counter = 0
loops_per_increment = 10000

def increment(name):
    global counter
    i = 0
    while i < loops_per_increment:
        counter += 1
        i += 1


if __name__ == '__main__':
    expected = 0
    threads = []
    try:
        for i in xrange(100):
           name = "Thread-" + str(i)
           t = threading.Thread( target=increment, args=(name,) )
           expected += loops_per_increment
           t.start()
           threads.append(t)
    except:
          print "Error: unable to start thread"

    for t in threads:
        t.join()
    print counter, "- expected:", expected

, 8- :

[mitalia@mitalia ~/scratch]$ for i in (seq 10)
                                 python inc.py 
                             end
47012 - expected: 1000000
65696 - expected: 1000000
51456 - expected: 1000000
44628 - expected: 1000000
52087 - expected: 1000000
50812 - expected: 1000000
53277 - expected: 1000000
49652 - expected: 1000000
73703 - expected: 1000000
53902 - expected: 1000000
+6

:

import sys
# Check for thread switches after every virtual instruction.
sys.setcheckinterval(0)

import threading

INCREMENTS = 1000000

counter = 0

def task():
  global counter
  for i in xrange(INCREMENTS):
    counter += 1

thread1 = threading.Thread(target=task)
thread2 = threading.Thread(target=task)

thread1.start()
thread2.start()

thread1.join()
thread2.join()

print "expected counter:", INCREMENTS * 2, "final counter:", counter
0

All Articles