我目前正试图让我的程序处理来自几个类的几个作业,但我无法让它实际执行process()函数。这是基于在网上找到的一个主题。在
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)
myThread.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 ("{0} processing {1}".format(threadName, data))
else:
queueLock.release()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = [myclass1.process, myclass2.process, myclass3.process, myclass5.process, myclass6.process]
queueLock = threading.Lock()
workQueue = queue.Queue()
threads = []
threadID = 1
exitFlag = 0
# 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")
这将按预期返回:
^{pr2}$我目前使用线程(目标=myclassX.流程)但现在不同了。。。 我如何修改我的程序,使其实际为每个类执行process()函数,而不是将其作为绑定方法添加到队列中?在
汤姆道尔顿在评论中给了我答案。在
相关问题 更多 >
编程相关推荐