this is not an answer, but was difficult to me to understand the question , so I slighty modified the code here; what I ended up with trying to understand what
print(count) # I have been getting 0 multiple times
means, in the OP question. Hope I got it right:
from threading import Thread
# BIG_NUMBER = 500000000
BIG_NUMBER = 5 #messo da me se no troppo lento
count = BIG_NUMBER
def dec(n):
global count
for _ in range(n):
count -= 1
results = []
for i in range(10):
BIG_NUMBER *= i+1
count = BIG_NUMBER
print(BIG_NUMBER, count)
t1 = Thread(target=dec, args=(BIG_NUMBER // 2,))
t2 = Thread(target=dec, args=(BIG_NUMBER // 2,))
t1.start()
t2.start()
t1.join()
t2.join()
# print(count) # I have been getting 0 multiple times
results.append(count)
print(results)
output:
5 5
10 10
30 30
120 120
600 600
3600 3600
25200 25200
201600 201600
1814400 1814400
18144000 18144000
[1, 0, 0, 0, 0, 0, 0, 46670, 557348, 6385685]
adding :
import sis
sys.setswitchinterval(0.5)
print(' sys.getswitchinterval() : ', sys.getswitchinterval())
at the beginnig of code, I got results more in the likenesss of:
output:
5 5
10 10
30 30
120 120
600 600
3600 3600
25200 25200
201600 201600
1814400 1814400
18144000 18144000
[1, 0, 0, 0, 0, 0, 0, 0, 0, 2626417]
see sys.setswitchinterval(interval)
sys.setswitchinterval(interval)
Set the interpreter’s thread switch interval (in seconds). This floating-point value determines the ideal duration of the “timeslices” allocated to concurrently running Python threads. Please note that the actual value can be higher, especially if long-running internal functions or methods are used. Also, which thread becomes scheduled at the end of the interval is the operating system’s decision. The interpreter doesn’t have its own scheduler.
New in version 3.2.
got from Can you race condition in Python while there is a GIL?
as explained in post below https://stackoverflow.com/a/74380851/9877065
ADDENDUM
following discussion about answer below : https://stackoverflow.com/a/74380851/9877065
I added 2 threads to the ones in the original script and seems like
result are conparable to the previous ones
##########################################################################################
# https://stackoverflow.com/questions/23855138/can-you-race-condition-in-python-while-there-is-a-gil/23855229#23855229
import sys
print(' sys.getswitchinterval() : ', sys.getswitchinterval())
######################################################################################
sys.setswitchinterval(0.5)
print(' sys.getswitchinterval() : ', sys.getswitchinterval())
from threading import Thread
# BIG_NUMBER = 500000000
BIG_NUMBER = 5 #messo da me se no troppo lento
count = BIG_NUMBER
def dec(n):
global count
for _ in range(n):
count -= 1
results = []
for i in range(10):
BIG_NUMBER *= i+1
# print(BIG_NUMBER)
count = BIG_NUMBER
print(BIG_NUMBER, count)
t1 = Thread(target=dec, args=(BIG_NUMBER // 4,))
t2 = Thread(target=dec, args=(BIG_NUMBER // 4,))
t3 = Thread(target=dec, args=(BIG_NUMBER // 4,))
t4 = Thread(target=dec, args=(BIG_NUMBER // 4,))
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
# print(count) # I have been getting 0 multiple times
results.append(count)
print(results)
typical output:
sys.getswitchinterval() : 0.005
sys.getswitchinterval() : 0.005
5 5
10 10
30 30
120 120
600 600
3600 3600
25200 25200
201600 201600
1814400 1814400
18144000 18144000
[1, 2, 2, 0, 0, 0, 0, 40218, 1005081, 11183482]
and
sys.getswitchinterval() : 0.005
sys.getswitchinterval() : 0.5
5 5
10 10
30 30
120 120
600 600
3600 3600
25200 25200
201600 201600
1814400 1814400
18144000 18144000
[1, 2, 2, 0, 0, 0, 0, 0, 0, 2628116]
any comment on this will be highly appreciated