对可能被threading.Lock锁定的项目使用random.choice

时间:2017-07-11 17:00:56

标签: python multithreading random

我正在开发一个搜索树代理,它可以使用多线程来增长其节点 每个节点都有这些功能:

import threading
def __init__(self, move=None, parent=None):    
  self.lock = threading.Lock()
  self.lock.acquire()
  self.move = move
  self.parent = parent
  self.N = 0  
  self.Q = 0
  self.children = {}
  self.lock.release()

正如您在初始化节点时所看到的那样,它被锁定以避免可能重复的节点 在我的代码的某个地方,我将父节点的所有节点放在一个列表中并随机选择:

max_nodes = [n for n in node.children.values()]
node = random.choice(max_nodes)

如果在创建max_nodes列表期间,节点被锁定(节点可能会不时被锁定),随机选择会发生什么。
它是否等待所有节点被释放? 我问这个问题,因为我多次得到这个错误:
cannot choose from an empty list

0 个答案:

没有答案