Grundlegende Muster für Nebenläufigkeit
Implementieren Sie einfache nebenläufige Anwendungen mit dem Starten von Prozessen und dem Empfangen von Nachrichten und erkunden Sie grundlegende Muster wie Client-Server-Interaktionen
Grundlegende Muster für Nebenläufigkeit ist eine kostenlose Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Grundlegende Muster für Nebenläufigkeit“ kostenlos?
Ja — der vollständige Text von „Grundlegende Muster für Nebenläufigkeit“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Grundlegende Muster für Nebenläufigkeit“?
Implementieren Sie einfache nebenläufige Anwendungen mit dem Starten von Prozessen und dem Empfangen von Nachrichten und erkunden Sie grundlegende Muster wie Client-Server-Interaktionen Du übst Erlang OTP: Distributed & Fault-Tolerant Systems Programming mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Erlang OTP: Distributed & Fault-Tolerant Systems Programming zu starten?
Keine Vorkenntnisse erforderlich. Erlang OTP: Distributed & Fault-Tolerant Systems Programming auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „Grundlegende Muster für Nebenläufigkeit“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lektion Code schreiben und ausführen?
Ja. Jede Erlang OTP: Distributed & Fault-Tolerant Systems Programming-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Einführung in Erlang und die VM
- Erlang-Prozesse und Messaging
- Grundlegende Muster für Nebenläufigkeit
- Pattern Matching und Guards