基本的な並行処理パターン
プロセスの生成とメッセージの受信を使って単純な並行アプリケーションを実装し、クライアント・サーバー通信などの基本パターンを学びます。
「基本的な並行処理パターン」はCoddyKit上の無料Erlang OTP: Distributed & Fault-Tolerant Systems Programmingレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはErlang OTP: Distributed & Fault-Tolerant Systems Programming学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Erlang OTP: Distributed & Fault-Tolerant Systems Programmingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Concurrency Patterns in Erlang
Concurrency patterns are proven ways to structure how processes interact, so you build robust, predictable concurrent apps.
The Request-Response Pattern
The Request-Response pattern is a conversation: a client sends a request, the server does the work, then replies. The backbone of interactive services.
Building a Simple Server
A server process loops on receive, handling each message and replying with SenderPid ! Reply; self() gives it its own PID.
Code: Echo Server Example
This echo server receives any message, grabs the sender's PID, and sends the message right back tagged as echoed.
-module(echo_server).
-export([start/0, loop/0]).
start() ->
spawn(echo_server, loop, []).
loop() ->
receive
{FromPid, Message} ->
FromPid ! {self(), echoed, Message},
loop();
_ ->
io:format("Server received unknown message.~n"),
loop()
end.Code: Echo Client Interaction
The matching echo client spawns the server, sends {self(), msg} so the server knows where to reply, then waits for the response.
-module(echo_client).
-export([run/0]).
run() ->
ServerPid = echo_server:start(),
io:format("Server started with PID: ~p~n", [ServerPid]),
Request = "Hello Erlang!",
ServerPid ! {self(), Request},
io:format("Client sent: ~p to server ~p~n", [Request, ServerPid]),
receive
{_ServerPid, echoed, Response} ->
io:format("Client received reply: ~p~n", [Response]);
_ ->
io:format("Client received unexpected message.~n")
after 5000 ->
io:format("Client timed out waiting for reply.~n")
end.Running the Echo System
To run it, compile both modules and call echo_client:run() in the shell — a full request-response cycle across two processes.
The Asynchronous Pattern
The Asynchronous ("fire and forget") pattern: the client sends a message and moves on immediately while the receiver works in the background. Great for logging or jobs.
Code: Asynchronous Logger Process
This logger process just receives messages and prints them, sending no reply — the server side of the asynchronous pattern.
-module(async_logger).
-export([start/0, loop/0]).
start() ->
spawn(async_logger, loop, []).
loop() ->
receive
{log, Message} ->
io:format("LOG: ~p~n", [Message]),
loop();
_ ->
io:format("Logger received unknown message.~n"),
loop()
end.Code: Asynchronous Logger Client
The matching client spawns the logger, sends a message, and continues immediately without waiting — non-blocking by design.
-module(logger_client).
-export([run/0]).
run() ->
LoggerPid = async_logger:start(),
io:format("Logger started with PID: ~p~n", [LoggerPid]),
LoggerPid ! {log, "User X logged in."},
io:format("Client sent first log. Continuing...~n"),
timer:sleep(10), % Give logger a moment to process
LoggerPid ! {log, "User Y viewed profile."},
io:format("Client sent second log. Task complete.~n").Concurrency Pattern Check
You're building a system where a user uploads a large video. Your main process needs to immediately tell the user "Video uploaded, processing in background." while a separate process transcodes the video. What pattern best describes the interaction between the main process and the video transcoder?
Recap: Basic Concurrency Patterns
Recap: Request-Response waits for a reply (interactive work), while Asynchronous fires and forgets (background tasks). Your core concurrency toolkit.
よくある質問
「基本的な並行処理パターン」レッスンは無料ですか?
はい。「基本的な並行処理パターン」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Erlang OTP: Distributed & Fault-Tolerant Systems Programmingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Erlang OTP: Distributed & Fault-Tolerant Systems Programmingコースには全4レッスンが含まれています。
「基本的な並行処理パターン」で何を学びますか?
プロセスの生成とメッセージの受信を使って単純な並行アプリケーションを実装し、クライアント・サーバー通信などの基本パターンを学びます。 ブラウザで直接実行するハンズオンコードでErlang OTP: Distributed & Fault-Tolerant Systems Programmingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Erlang OTP: Distributed & Fault-Tolerant Systems Programmingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのErlang OTP: Distributed & Fault-Tolerant Systems Programmingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「基本的な並行処理パターン」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このErlang OTP: Distributed & Fault-Tolerant Systems Programmingレッスンでコードを書いて実行できますか?
はい。すべてのErlang OTP: Distributed & Fault-Tolerant Systems Programmingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ErlangとVM入門
- Erlangのプロセスとメッセージング
- 基本的な並行処理パターン
- パターンマッチングとガード