writings/10/6/2022

(Intro to Processes in Elixir)

So today during an Elixir code meetup group we went over processes in Elixir. I would just like to distill what I learned/reviewed here. I won't cover nearly anywhere near as much so be sure to check out the linked documentation.

A process in elixir can be start with the kernel function spawn

Spawn A Process

We can write following in iEX.

some_pid = spawn(fn  -> 3 end)  
 
 

#PID<0.112.0>

So spawn takes a function and starts a process running that function.

Now we have the basic process started, what can we do with it.

Well we can send messages to processes.

Communicating With Processes

Send Message to Process

iex> send(some_pid,"Hello") 

"Hello"

So send takes a PID, or local port and a bunch of other stuff I won't discuss along with a message. It then returns the message sent. Well that's not useful right now because our process isn't doing anything with the message.

Receive a Message

Lets create a new process

iex> new_pid = spawn(fn -> receive do "Hello" -> IO.puts("Hi") end end)  

#PID<0.125.0>

And lets send it a message of "Hello"

iex> send(new_pid,"Hello") 

Hello

Hi

So we first see "Hello" as expected since that was our message. And then quickly after we see "Hi".

Conclusion

That's the basics of communicating with processes in Elixir, I may talk more about processes in the future. There are tons more stuff you can do we might talk about GenServers,. I'm not sure how deep the rabbit hole I want to go