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

2024-05-03 04:34:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个搜索树代理,它可以使用多线程来扩展节点。
每个节点都有以下功能:

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)

如果在创建最大\u节点列表的过程中,某个节点被锁定(节点可能会不时被锁定),那么随机选择会发生什么情况。
是否等待所有节点释放?
我问这个问题是因为我多次遇到这个错误:
cannot choose from an empty list


Tags: importself功能nonenodelock代理列表