» Keep Your Thread or Don't
In Raku, there are different ways to pause your code. The simplest and most
obvious way to do so is to use
sleep:
my $before = now;
sleep 1;
my $after = now - $before;
say $after;
Assuming your system is not bogged down at the moment, the output of $after
should be a number pretty close to 1. Not very exciting: you didn’t do anything
for a second. Woo. Woo.
In real code, however, you do sometimes need to pause. For example, you might be trying to send an email and it fails. When sending email, it is nice if it arrives quickly, but essential that it arrives eventually. Thus, when queueing up a message, you want to watch for errors. When they occur, you want to keep trying for a long time before giving up. However, you don’t want to keep trying constantly. You need to pause between retries: