Processes
From Reia
Processes are Reia's concurrency primitive. While in many ways they can be thought of as threads, they do not share state and thus are more closely related to Unix-style processes.
Contents |
Creating processes
The process module provides two functions for creating processes, Process.spawn and Process.spawn_link. Each of these functions takes a block:
>> pid = Process.spawn { 2 + 2 }
=> <0.31.0>
The spawn and spawn_link functions return the process identifier, or pid, of the newly created processes. This identifier can be used to send messages to a process.
The spawn_link function differs from spawn by linking the newly created process to the current one.
Messaging
To send a process a message, use the ! operator:
>> pid ! 'hi' => 'hi'
Receiving messages
To receive messages, a process must invoke the receive keyword. The syntax of receive is similar to case:
receive when firstPattern ... when secondPattern ... end
The receive operation uses pattern matching to determine of any messages received should be processed. The code associated with the first pattern to match is executed, just like the case statement. If none of the patterns match, the process sleeps until it receives a new message.
Receiving messages in objects
Objects receive messages through the handle_message method:
>> class Foo .. def handle_message(msg) .. ["I got a message: ", msg].join().puts() .. end .. end .. => Foo >> foo = Foo() => #<Foo:0.78.0> >> foo ! "Why hello there" I got a message: Why hello there => "Why hello there"

