[Python] race condition issue in python

Discussão em 'Python' iniciado por Stack, Maio 26, 2025.

  1. Stack

    Stack Membro Participativo

    i want to consider following simple code :

    from multiprocessing import Process,Value
    import time
    def add_10_times(number):
    for i in range(10):
    time.sleep(0.01)
    number.value+=1
    if __name__ =="__main__":
    shared_number =Value('i',0)
    process1 = Process(target=add_10_times, args=(shared_number,))
    process2 = Process(target=add_10_times, args=(shared_number,))
    process1.start()
    process2.start()
    process1.join()
    process2.join()
    print(f'final value is {shared_number.value}')


    based on the following site : multiprocessing

    and based on well known problem called as Race condition, value should be less then 20 right? but in most cases it gives me value 20 :

    final value is 20


    sometimes it jumps to 16 or 18 , but in most case it gives me 20, so where is Race condition? and under what case it occurs?

    Continue reading...

Compartilhe esta Página