Receive with a Timeout
Sometimes a receive statement might wait forever for a message that never comes. This could be for a number of reasons. For example, there might be a logical error in our program, or the process that was going to send us a message might have crashed before it sent the message. To avoid this problem, we can add a timeout to the receive statement. This sets a maximum time that the process will wait to receive a message. The syntax is as follows:
| receive |
| Pattern1 [when Guard1] -> |
| Expressions1; |
| Pattern2 [when Guard2] -> |
| Expressions2; |
| ... |
| after Time -> |
| Expressions |
| end |
If no matching message has arrived within Time
milliseconds of entering the receive expression, then the process will stop waiting ...
Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.