do you want to create these sub groups before training loop, and each iteration just uses corresponding created sub groups? instead of creating the same sub groups repeatedly inside the loop?
also, add time out if it hangs?
lastly, maybe need to figure out why it hangs, starting with debugging using a small number of training iterations?