Erlang 프로세스 및 메시징
Erlang의 핵심 동시성 기본 요소인 프로세스를 이해합니다. 비동기 메시지 전달로 프로세스가 통신하는 방법을 배우고 격리된 경량 동시 실행 단위를 구축합니다.
Erlang 프로세스 및 메시징은(는) CoddyKit의 무료 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Erlang's Core: The Process
The fundamental unit of Erlang concurrency is the process: a tiny, independent program running alongside others, far lighter than an OS thread.
Why Erlang Processes?
Erlang processes are lightweight (millions per machine), isolated (no shared memory), concurrent, and talk only via async messages.
Spawning a New Process
You create a process with spawn, passing a module, function, and args. It returns a unique PID identifying the new process.
-module(process_spawn).
-export([start/0, hello_world_process/0]).
hello_world_process() ->
io:format("Hello from a new Erlang process!~n").
start() ->
% Spawns a new process running hello_world_process/0
Pid = spawn(process_spawn, hello_world_process, []),
io:format("Spawned process with PID: ~p~n", [Pid]).Understanding Process IDs (PIDs)
Every process gets a unique PID — its postal address. You need that PID to send it any message; it looks like <0.80.0>.
Sending Messages with '!'
Processes talk by messages, never shared memory. Send one with the ! operator: Pid ! Message, where the message is any Erlang term.
-module(message_sender).
-export([start/0, receiver_process/0, sender_process/1]).
receiver_process() ->
io:format("Receiver process ~p started.~n", [self()]),
% This process will just print its PID for now.
% It doesn't receive messages in this version.
timer:sleep(1000). % Keep process alive for a moment
sender_process(ReceiverPid) ->
io:format("Sender process ~p sending message to ~p...~n", [self(), ReceiverPid]),
ReceiverPid ! {hello, "from sender"},
io:format("Message sent!~n").
start() ->
ReceiverPid = spawn(message_sender, receiver_process, []),
io:format("Receiver spawned with PID: ~p~n", [ReceiverPid]),
sender_process(ReceiverPid).Receiving Messages: 'receive'
A process pulls messages from its mailbox with receive, which pattern-matches each one and waits (or times out) until a match arrives.
-module(message_receiver).
-export([start/0, my_receiver/0, my_sender/1]).
my_receiver() ->
io:format("Receiver ~p waiting for messages...~n", [self()]),
receive
{hello, Msg} ->
io:format("Receiver ~p received: ~p~n", [self(), Msg]);
_AnyOtherMessage ->
io:format("Receiver ~p received an unexpected message: ~p~n", [self(), _AnyOtherMessage])
end,
io:format("Receiver ~p finished.~n", [self()]).
my_sender(ReceiverPid) ->
io:format("Sender ~p sending message to ~p.~n", [self(), ReceiverPid]),
ReceiverPid ! {hello, "World"},
io:format("Sender ~p sent message.~n", [self()]).
start() ->
ReceiverPid = spawn(message_receiver, my_receiver, []),
timer:sleep(100), % Give receiver time to start
my_sender(ReceiverPid).A Full Messaging Example
Here is the full flow in one example: spawn a server, the client sends a request, and the server receives and replies.
-module(full_message_example).
-export([start/0, server_loop/0, client_action/1]).
server_loop() ->
io:format("Server ~p started, waiting for requests...~n", [self()]),
receive
{request, ClientPid, Message} ->
io:format("Server ~p received request from ~p: ~p~n", [self(), ClientPid, Message]),
ClientPid ! {response, self(), "Got your message!"};
_Other ->
io:format("Server ~p received unexpected: ~p~n", [self(), _Other])
end.
client_action(ServerPid) ->
io:format("Client ~p sending request to server ~p...~n", [self(), ServerPid]),
ServerPid ! {request, self(), "Can you hear me?"},
receive
{response, ServerPid, Reply} ->
io:format("Client ~p received reply from ~p: ~p~n", [self(), ServerPid, Reply])
end.
start() ->
ServerPid = spawn(full_message_example, server_loop, []),
timer:sleep(100), % Give server time to start
client_action(ServerPid).Asynchronous Communication
Erlang messaging is asynchronous: the sender drops the message in the mailbox and keeps going, never blocking on the receiver.
The Process Mailbox
Each process has a private mailbox where incoming messages queue. A receive scans it for a pattern match, usually in arrival order.
Knowing Your Own PID: `self()`
The built-in self() returns the current process's PID — handy for messaging yourself or embedding a reply address in a message.
-module(self_example).
-export([start/0, print_self/0]).
print_self() ->
io:format("Hello, my PID is: ~p~n", [self()]).
start() ->
io:format("Parent process PID: ~p~n", [self()]),
spawn(self_example, print_self, []).
Quick Check: Message Flow
Consider the following sequence of events in Erlang:
- Process A spawns Process B.
- Process A sends a message to Process B.
- Process B receives the message.
Which of the following statements about this interaction are true?
Recap: Processes & Messaging
Recap: processes communicate by async messages — spawn returns a PID, ! sends, receive matches, each has a mailbox, and self() gives your own PID.
자주 묻는 질문
“Erlang 프로세스 및 메시징” 강의는 무료인가요?
네 — “Erlang 프로세스 및 메시징” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.
“Erlang 프로세스 및 메시징”에서 뭘 배우나요?
Erlang의 핵심 동시성 기본 요소인 프로세스를 이해합니다. 비동기 메시지 전달로 프로세스가 통신하는 방법을 배우고 격리된 경량 동시 실행 단위를 구축합니다. 브라우저에서 직접 실행하는 실습 코드로 Erlang OTP: Distributed & Fault-Tolerant Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Erlang OTP: Distributed & Fault-Tolerant Systems Programming을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Erlang OTP: Distributed & Fault-Tolerant Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.
“Erlang 프로세스 및 메시징” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- Erlang 및 VM 소개
- Erlang 프로세스 및 메시징
- 기본 동시성 패턴
- 패턴 매칭과 가드