小编x.w*_*ang的帖子

当我放入或从中取出时,我是否应该打扰锁定队列?

我已经在python3中学习了关于多线程和队列的教程.正如官方教程所说,"此模块中的Queue类实现了所有必需的锁定语义".但是在另一个教程中,我看到了一个如下例子:

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", …
Run Code Online (Sandbox Code Playgroud)

python multithreading

7
推荐指数
1
解决办法
3845
查看次数

标签 统计

multithreading ×1

python ×1