0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · درس

أنماط التزامن الأساسية

نفّذ تطبيقات متزامنة بسيطة باستخدام إنشاء العمليات واستقبال الرسائل، واستكشف أنماطًا أساسية مثل تفاعلات العميل والخادم

أنماط التزامن الأساسية درس مجاني في Erlang OTP: Distributed & Fault-Tolerant Systems Programming على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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/7) وفتح باقي دورة 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/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Erlang OTP: Distributed & Fault-Tolerant Systems Programming؟

لا تُشترط خبرة سابقة. Erlang OTP: Distributed & Fault-Tolerant Systems Programming على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

كم من الوقت يستغرق درس «أنماط التزامن الأساسية»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Erlang OTP: Distributed & Fault-Tolerant Systems Programming هذا؟

نعم. كل درس في Erlang OTP: Distributed & Fault-Tolerant Systems Programming يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى Erlang والآلة الافتراضية
  2. عمليات Erlang وتبادل الرسائل
  3. أنماط التزامن الأساسية
  4. مطابقة الأنماط وعبارات الحراسة
← العودة إلى Erlang OTP: Distributed & Fault-Tolerant Systems Programming