我正在关注tutorialspoint,在他们的情况下,他们在我复制粘贴它时写它,但是当我试图在我的pycharm中重现它时,它给出了错误确实,IF不必要地缩进。因此,我缩减,但是当我这样做时,我没有得到同步线程只有线程3做了一些事情,线程1和2刚刚开始但从未执行任何工作,然后全部离开。但是,显然,它适用于教程作者,他们不会缩进IF。我正在使用python 3,所以我不得不小写队列导入。
import Queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print "Starting " + self.name
process_data(self.name, self.q)
print "Exiting " + self.name
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty(): #THIS IS THE ONE THEY INDENT
data = q.get()
queueLock.release()
print "%s processing %s" % (threadName, data)
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = Queue.Queue(10)
threads = []
threadID = 1
# Create new threads
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print "Exiting Main Thread"
答案 0 :(得分:1)
您找到了答案,但仅举例说明,这是一个简化版本,不需要全局退出标志并删除不必要的锁定:
import queue
import threading
import time
def process_data():
name = threading.current_thread().name
print('Starting',name)
while True:
data = q.get()
if data is None: # Check for exit special value
break
print(name,'processing',data)
time.sleep(1)
q.task_done()
print('Exiting',name)
thread_count = 3
nameList = ["One", "Two", "Three", "Four", "Five"]
q = queue.Queue()
# Create new threads
threads = [threading.Thread(target=process_data) for _ in range(thread_count)]
for t in threads:
t.start()
# Fill the queue
for word in nameList:
q.put(word)
# Wait for queue to empty
q.join()
# Notify threads it's time to exit
for t in threads:
q.put(None) # Special value put in queue indicating to exit
# Wait for all threads to complete
for t in threads:
t.join()
print('Exiting main thread')
输出:
Starting Thread-1
Starting Thread-2
Starting Thread-3
Thread-1 processing One
Thread-3 processing Three
Thread-2 processing Two
Thread-3 processing Four
Thread-2 processing Five
Exiting Thread-1
Exiting Thread-3
Exiting Thread-2
Exiting main thread
答案 1 :(得分:0)
嗯,结论是Tutorialspoint的例子在IF缩进时有错误。不应该有这样的事情。这意味着他们显示的结果明确地与代码不对应。
所以:
我做了两处改动: 首先,我缩小了IF和其他人以及睡眠。 其次,如果我还没有创建它,我无法看到我如何调用工作队列。所以我将队列创建的片段移到了调用之上。
这就是它在Python 3中的应用:
import queue
import threading
import time
exitFlag = 0
class myThread (threading.Thread):
def __init__(self, threadID, name, q):
threading.Thread.__init__(self)
self.threadID = threadID
self.name = name
self.q = q
def run(self):
print ("Starting" + self.name)
process_data(self.name, self.q)
print ("Exiting" + self.name)
def process_data(threadName, q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data = q.get()
queueLock.release()
print ("%s processing %s" % (threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10)
threads = []
threadID = 1
# Fill the queue
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# Create new threads
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# Wait for queue to empty
while not workQueue.empty():
pass
# Notify threads it's time to exit
exitFlag = 1
# Wait for all threads to complete
for t in threads:
t.join()
print ("Exiting Main Thread")
我得到的结果(看起来不错)是:
StartingThread-1
Thread-1 processing One
StartingThread-2
Thread-2 processing Two
StartingThread-3
Thread-3 processing Three
Thread-1 processing Four
Thread-2 processing Five
ExitingThread-3
ExitingThread-1
ExitingThread-2
Exiting Main Thread