【发布时间】:2017-02-16 17:00:15
【问题描述】:
谁能指出这段代码有什么问题。我试图通过一个变量标志返回线程,我想在我的主线程中控制它。
test27.py
import threading
import time
lock = threading.Lock()
def Read(x,y):
flag = 1
while True:
lock.acquire()
try:
z = x+y; w = x-y
print z*w
time.sleep(1)
if flag == 0:
print "ABORTING"
return
finally:
print " SINGLE run of thread executed"
lock.release()
test28.py
import time, threading
from test27 import Read
print "Hello Welcome"
a = 2; b = 5
t = threading.Thread(target = Read, name = 'Example Thread', args = (a,b))
t.start()
time.sleep(5)
t.flag = 0 # This is not updating the flag variable in Read FUNCTION
t.join() # Because of the above command I am unable to wait until the thread finishes. It is blocking.
print "PROGRAM ENDED"
【问题讨论】:
标签: python multithreading time