Giter VIP home page Giter VIP logo

Comments (3)

ironboxer avatar ironboxer commented on June 22, 2024 2

python3.10中的新变化:
https://stackoverflow.com/questions/69993959/python-threads-difference-for-3-10-and-others

demo:

from threading import Thread

i = 0

def foo():
    global i
    for _ in range(1000000):
        i += 1

def bar():
    global i
    for _ in range(1000000):
        i -= 1
    
threads = []    
for _ in range(10):
    thread = Thread(target=foo)
    threads.append(thread)
for _ in range(10):
    thread = Thread(target=bar)
    threads.append(thread)
 
for thread in threads:
    thread.start()

for thread in threads:
    thread.join()


print(i)
assert i == 0

我是认为,这从侧面说明“Python还不是一个很稳定的语言”,像这种特性,无论是否是原子的,早就应该固定下来,不发生变化。

from one-python-craftsman.

piglei avatar piglei commented on June 22, 2024

你好,value 是局部或是全部不是重点,重点在于 "+=" 运算符不是原子。在它的执行过程中有可能会被插入其他语句,所以不是线程安全的。

from one-python-craftsman.

piglei avatar piglei commented on June 22, 2024

python3.10中的新变化: https://stackoverflow.com/questions/69993959/python-threads-difference-for-3-10-and-others

demo:

from threading import Thread

i = 0

def foo():
    global i
    for _ in range(1000000):
        i += 1

def bar():
    global i
    for _ in range(1000000):
        i -= 1
    
threads = []    
for _ in range(10):
    thread = Thread(target=foo)
    threads.append(thread)
for _ in range(10):
    thread = Thread(target=bar)
    threads.append(thread)
 
for thread in threads:
    thread.start()

for thread in threads:
    thread.join()


print(i)
assert i == 0

我是认为,这从侧面说明“Python还不是一个很稳定的语言”,像这种特性,无论是否是原子的,早就应该固定下来,不发生变化。

很有意思,3.10 的这个改动我也是第一次看到。这小部分内容我没有写进图书版里,如果之后修订,我考虑调整下措辞或示例来加强说服力。大观点会维持不变:不依赖任何直觉看上去像(未文档化的)“原子操作”的行为实现并发安全。

from one-python-craftsman.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.