From where do you want to terminate the loop? Can't you use `break` inside the loop? I.e., you stop after the desired number of epochs has been received.