Podstawowe wzorce współbieżności
Implementuj proste aplikacje współbieżne z użyciem uruchamiania procesów i odbierania komunikatów oraz poznaj podstawowe wzorce, takie jak interakcje klient–serwer.
Podstawowe wzorce współbieżności to bezpłatna lekcja Erlang OTP: Distributed & Fault-Tolerant Systems Programming na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Erlang OTP: Distributed & Fault-Tolerant Systems Programming, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „Podstawowe wzorce współbieżności” jest bezpłatna?
Tak — pełny tekst „Podstawowe wzorce współbieżności” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Erlang OTP: Distributed & Fault-Tolerant Systems Programming, przejdź na CoddyKit PRO. Kurs Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera 4 lekcji w sumie.
Co nauczysz się w „Podstawowe wzorce współbieżności”?
Implementuj proste aplikacje współbieżne z użyciem uruchamiania procesów i odbierania komunikatów oraz poznaj podstawowe wzorce, takie jak interakcje klient–serwer. Ćwiczysz Erlang OTP: Distributed & Fault-Tolerant Systems Programming z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Nie wymagamy żadnego doświadczenia. Erlang OTP: Distributed & Fault-Tolerant Systems Programming w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.
Ile czasu zajmuje lekcja „Podstawowe wzorce współbieżności”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Erlang OTP: Distributed & Fault-Tolerant Systems Programming?
Tak. Każda lekcja Erlang OTP: Distributed & Fault-Tolerant Systems Programming zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wprowadzenie do Erlanga i maszyny wirtualnej
- Procesy i komunikacja w Erlangu
- Podstawowe wzorce współbieżności
- Dopasowywanie wzorców i strażnicy