[英]Is using an 'anonymous' threading.Lock() always an error?
我試圖理解一些代碼,我在下面看到這個 function
def get_batch(
self,
) -> Union[Tuple[List[int], torch.Tensor], Tuple[None, None]]:
"""
Return an inference batch
"""
with threading.Lock():
indices: List[int] = []
for _ in range(self.batch_size):
try:
index = self.full_queue.get(timeout=0.05)
indices.append(index)
except:
break
if indices:
# tqdm.write(str(len(jobs)))
batch = {
key: torch.stack([self.input_buffers[key][index] for index in indices])
.to(torch.device('cpu'), non_blocking=True)
.unsqueeze(0)
for key in self.input_buffers
}
return indices, batch
else:
return None, None
with threading.Lock()
行一定是個錯誤吧? 一般來說,鎖必須共享,而這不與任何東西共享?
是的,@Homer512 的評論一語中的。 每次激活 function 都會創建一個新Lock
object,並且這些對象無法在線程之間共享。 鎖定無法被任何其他線程鎖定的Lock
不會完成任何事情。 這實際上是一個空操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.