Using Python 2.7.3 on Windows.
How can I share a variable num
between threads, such that, after num
is squared, it is printed?
I realized that I need to understand how threads work, but docs don't have much, and I haven't found anything here either..
So, could someone explain how threads work and how to share variables between 2 threads?
My code (keeps printing 2
)
import threading
def func1(num):
while num < 100000000:
num = num**2
def func2(num):
while num < 100000000:
print num,
num = 2
thread1 = threading.Thread(target=func1,args=(num,))
thread2 = threading.Thread(target=func2,args=(num,))
print 'setup'
thread1.start()
thread2.start()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…