基本并发模式
使用创建进程和接收消息实现简单的并发应用,并探索客户端-服务器交互等基本模式。
基本并发模式 是 CoddyKit 上的免费 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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.
常见问题解答
「基本并发模式」课时是免费的吗?
是的 — 「基本并发模式」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 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,全天候 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 反馈 — 无需本地设置。