0

Just forget about title....

I am trying, to print global variables with 5 iterations for each thread. On each iteration, global variables value increment by 5000.
I want that each thread reads the same variable value as previous or reads variable value by one thread does not effect the variable value read by another thread.

Example:

If thread-1 reads f_price = 1000 and l_price = 6000, then after increment f_price = 6000 and l_price = 11000 became respectively. So increment by thread-1 does not effect the value reads by the thread-2. Thread-2 first read the same variable values f_price = 1000 and l_price = 6000. and so on.....

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()
  • Does this answer your question? [Python creating a shared variable between threads](https://stackoverflow.com/questions/17774768/python-creating-a-shared-variable-between-threads) – njzk2 Jan 28 '22 at 23:02
  • @njzk2 my problem is quite different then this, on each iteration it increase value by 5000 so, in my case their is no way back like you comment question. There are actually total 6 threads in example code I just mention 3 o them. – Mateen Aslam Jan 28 '22 at 23:16
  • I see. I didn't see "one thread does not (sic) effect the variable". In which case, using global variables makes no sense. You can't have global variable that are not, well, global – njzk2 Jan 29 '22 at 19:43
  • then best way to use? – Mateen Aslam Jan 30 '22 at 03:58

1 Answers1

0

Have main accept two arguments rather than using (and modifying) the global variables.

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()
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Working...., Thanks for being here. But still I want to know is their any way we use global value as I explain in above? – Mateen Aslam Jan 29 '22 at 06:57
  • No, because you explicitly want each thread's changes to be hidden from the other threads. (Locks and other synchronization schemes won't help, because their purpose is to simply prevent two threads from accessing the same variable at the same time, not to keep changes hidden from other threads.) – chepner Jan 29 '22 at 16:08
  • okay Can you see this problem: https://stackoverflow.com/questions/70824428/how-to-suspend-and-resume-all-running-threads-on-certain-condition-in-python?noredirect=1#comment125210455_70824428 – Mateen Aslam Jan 29 '22 at 17:02