![]() The proper way to read the mailbox, which gives you a chance to do something with the messages, is the receive.end construct. The first time you use it you’ll get a report of what’s in the mailbox, but the second time the messages are gone, already read: There’s a shell function- flush()-that you can use to see what’s in the mailbox, but it also removes those messages from the mailbox. Where did those messages go? What happened to them? Right now, they’re just waiting in the shell’s mailbox, doing nothing. (The send/2 function always returns the message, which is why it appears right after the sends in lines 2 and 4.) ![]() Line 3 assigned the pid for the shell, retrieved with the self() function, to a variable named pid, and then line 4 used that pid variable to send a message containing the atom :test2. ![]() Line 2 sent a message to the shell containing the atom :test1. You use the send/2 function with two arguments, an expression containing the pid and the message: The syntax for sending a message is pretty simple. You need to make sure your processes are assembled correctly. It also won’t report that a message was ignored by a process. When that process gets time to check its mailbox, it will be able to retrieve and process the messages there.Įlixir, however, will never report that a message send failed, even if the pid doesn’t point to a real process. Your programs will send messages from one process to another by sending them to a pid. You’ll need to do more work to set up a cluster, but you won’t have to throw away code you wrote with pids and processes built on them when you get there.Įvery process gets its own pid, and those pids function like addresses for mailboxes. Pids can even identify processes running on different computers within a cluster. Elixir much prefers that you treat pids as abstractions. Elixir uses pids internally, but while you can read them in the shell, you can’t type pids directly into the shell or into functions. This group of numbers is guaranteed to be unique within this run of Elixir, not permanently the same in future use. You may get a different set of numbers when you try it. #PID is the shell’s representation of a process identifier the three integers constitute a triple that provides a unique identifier for this process. The easiest pid to get is your own, so in the shell you can just try the self() function: The first thing to explore is the process identifier, often called a pid. ![]() None of the previous examples sent or received messages, of course, but the shell is an easy place to send and (for test purposes, at least) receive messages. You’ve been working within a single process throughout this book so far, the Elixir shell. Get a free trial today and find answers on the fly, or master something new and useful. Join the O'Reilly online learning platform. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |