一个线程不会影响另一个线程?

One thread does not effect another other thread?

忘了标题....

我正在尝试为每个 线程 打印 5 次迭代 的全局变量。在每次迭代中,全局变量值递增 5000.
我希望每个线程读取与之前相同的变量值 一个线程读取变量值不影响另一个线程读取的变量值。

示例:

如果thread-1读到f_price = 1000和l_price = 6000,那么增量后f_price = 6000和l_price = 11000分别变成了。因此线程 1 的增量不会影响线程 2 读取的值。 Thread-2 首先读取相同的变量值 f_price = 1000 和 l_price = 6000. 依此类推.....

from threading import Thread
import threading
from time import sleep

# variables

f_price = 1000
l_price = 6000


def main():
    global f_price, l_price

    print(threading.currentThread().getName(), 'just Start the Job')
    
    for each_iteration in range(1,5):
        print('Hi I am', threading.currentThread().getName(), 'Price Lies Between is:', f_price, '-', l_price)
        print('\n')

        f_price = l_price
        l_price += 5000

    print(threading.currentThread().getName(), 'Finish the Job')


thread1 = Thread(target=main)
thread2 = Thread(target=main)
thread3 = Thread(target=main)

thread1.start()
thread2.start()
thread3.start()

thread1.join()
thread2.join()
thread3.join()

main 接受两个参数而不是使用(和修改)全局变量。

from threading import Thread
import threading
from time import sleep

# variables

f_price = 1000
l_price = 6000


def main(f_price, l_price):

    print(threading.currentThread().getName(), 'just Start the Job')
    
    for each_iteration in range(1,5):
        print('Hi I am', threading.currentThread().getName(), 'Price Lies Between is:', f_price, '-', l_price)
        print('\n')

        f_price = l_price
        l_price += 5000

    print(threading.currentThread().getName(), 'Finish the Job')


thread1 = Thread(target=main, args=(f_price, l_price)
thread2 = Thread(target=main, args=(f_price, l_price)
thread3 = Thread(target=main, args=(f_price, l_price)

thread1.start()
thread2.start()
thread3.start()

thread1.join()
thread2.join()
thread3.join()