0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

กระบวนการและการส่งข้อความใน Elixir

เรียนรู้เกี่ยวกับกระบวนการ Elixir ที่มีน้ำหนักเบา วิธีที่กระบวนการสื่อสารกัน และหลักการทำงานพร้อมกันแบบ 'ไม่แบ่งปันสิ่งใด'

กระบวนการและการส่งข้อความใน Elixir เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Meet Elixir Processes

Elixir is built for concurrency, and processes are its core building blocks. Think of them as tiny, isolated programs running simultaneously.

Unlike operating system processes or threads, Elixir processes are incredibly lightweight. You can have hundreds of thousands, even millions, running on a single machine!

  • Isolation: Each process has its own memory.
  • Communication: They talk to each other by sending messages.
  • Fault Tolerance: If one crashes, it doesn't bring down others.

Creating New Processes

We create a new Elixir process using the spawn function. It takes a function (often an anonymous function) that the new process will execute.

spawn returns a Process Identifier (PID), which is like an address for the new process.

defmodule MyModule do
  def greet do
    IO.puts "Hello from a new process!"
  end

  def run do
    # Spawn a new process that calls MyModule.greet()
    pid = spawn(MyModule, :greet, [])
    IO.puts "Spawned process with PID: #{inspect(pid)}"
  end
end

MyModule.run()

Understanding Process IDs (PIDs)

A Process Identifier, or PID, is a unique reference to an Elixir process. It's how you address a specific process to send it messages.

Think of a PID like a phone number for a person. You dial their number to talk to them, and processes use PIDs to communicate.

  • Every process has a unique PID.
  • PIDs are essential for message passing.
  • You can inspect a PID to see its internal representation.

Finding Your Own PID

Just like you can get the PID of a newly spawned process, any running process can find its own PID using the self() function.

This is crucial when a process needs to tell others how to send messages back to it.

defmodule PidDemo do
  def show_self_pid do
    IO.puts "My PID is: #{inspect(self())}"
  end

  def run do
    IO.puts "Main process PID: #{inspect(self())}"
    # Spawn a process to show its own PID
    spawn(PidDemo, :show_self_pid, [])
    :timer.sleep(100) # Give the spawned process time to run
  end
end

PidDemo.run()

Communicating with Messages

Elixir processes communicate by sending and receiving messages. This is the core of the 'share nothing' concurrency model.

Messages are simple Elixir terms (any data type) sent from one process to another's "mailbox."

  • send(pid, message): Puts message into the mailbox of the process identified by pid.
  • receive do ... end: Waits for messages in the current process's mailbox.

Answering Back

Let's see how a "client" process can send a message to a "server" process and receive a reply.

The server process uses a receive block to wait for and handle incoming messages. It then sends a reply back to the client's PID.

defmodule EchoServer do
  def loop do
    receive do
      {:echo, client_pid, message} ->
        send(client_pid, {:reply, message})
        loop() # Continue looping to receive more messages
    end
  end
end

defmodule Client do
  def run do
    # Start the server process
    server_pid = spawn(EchoServer, :loop, [])
    IO.puts "Echo server started with PID: #{inspect(server_pid)}"

    # Send a message to the server, including our own PID for reply
    send(server_pid, {:echo, self(), "Hello, server!"})
    IO.puts "Client sent 'Hello, server!' to #{inspect(server_pid)}"

    # Wait for a reply
    receive do
      {:reply, message} ->
        IO.puts "Client received reply: '#{message}'"
    end
  end
end

Client.run()

Why Share Nothing Matters

Elixir processes embody the 'share nothing' principle. This means processes do not share memory or state directly.

Instead, they communicate exclusively through message passing. This design choice offers significant benefits:

  • Isolation: Prevents one process from corrupting another's data.
  • Concurrency: Easier to reason about and scale across multiple CPU cores.
  • Fault Tolerance: A crash in one process doesn't affect others, making systems more robust.

Smart Message Handling

The receive block is incredibly powerful because it uses Elixir's pattern matching.

You can define different clauses within receive to match specific message structures, ignoring others until a match is found.

defmodule SmartReceiver do
  def loop do
    receive do
      {:greet, name} ->
        IO.puts "Hello, #{name}!"
        loop()
      {:farewell, name} ->
        IO.puts "Goodbye, #{name}!"
        loop()
      :quit ->
        IO.puts "Receiver quitting."
      _ -> # Catch-all for unmatched messages
        IO.puts "Received an unknown message."
        loop()
    end
  end

  def run do
    receiver_pid = spawn(SmartReceiver, :loop, [])
    send(receiver_pid, {:greet, "Alice"})
    send(receiver_pid, "Just a string")
    send(receiver_pid, {:farewell, "Bob"})
    send(receiver_pid, :quit)
    :timer.sleep(100) # Give processes time to finish
  end
end

SmartReceiver.run()

Keeping State with Recursion

Since processes don't share memory, how do they maintain state? Through recursion!

A process passes its current state as an argument to itself when it calls its loop function again. This creates an immutable, sequential flow of state changes.

defmodule Counter do
  def loop(count) do
    receive do
      :increment ->
        IO.puts "Incrementing to #{count + 1}"
        loop(count + 1)
      :get_count ->
        IO.puts "Current count: #{count}"
        loop(count)
      :stop ->
        IO.puts "Counter stopped at #{count}"
    end
  end

  def run do
    counter_pid = spawn(Counter, :loop, [0])
    send(counter_pid, :increment)
    send(counter_pid, :increment)
    send(counter_pid, :get_count)
    send(counter_pid, :stop)
    :timer.sleep(100)
  end
end

Counter.run()

Processes & Messages Check

Consider the following Elixir code snippet:

defmodule Quiz do
  def server_loop do
    receive do
      {:ping, client_pid} ->
        send(client_pid, :pong)
        server_loop()
    end
  end

  def client_action(server_pid) do
    send(server_pid, {:ping, self()})
    receive do
      :pong ->
        IO.puts "Received pong!"
      _ ->
        IO.puts "Received something else."
    end
  end

  def run do
    server_pid = spawn(Quiz, :server_loop, [])
    client_action(server_pid)
  end
end

Quiz.run()

What will be printed to the console when Quiz.run() is executed?

Your First Steps in Concurrency

Congratulations! You've taken a big step into Elixir's powerful concurrency model.

Here's a quick summary of what we covered:

  • Elixir Processes: Lightweight, isolated execution units.
  • PIDs: Unique identifiers for processes, used for addressing.
  • spawn & self(): Functions to create processes and get their PIDs.
  • Message Passing: The primary way processes communicate using send and receive.
  • 'Share Nothing': Processes don't share memory, leading to robust and scalable systems.
  • Pattern Matching: Used within receive to handle different message types.
  • State: Maintained through recursive function calls.

These fundamental concepts are the bedrock for building highly concurrent and fault-tolerant applications in Elixir!

คำถามที่พบบ่อย

บทเรียน “กระบวนการและการส่งข้อความใน Elixir” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กระบวนการและการส่งข้อความใน Elixir” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กระบวนการและการส่งข้อความใน Elixir”

เรียนรู้เกี่ยวกับกระบวนการ Elixir ที่มีน้ำหนักเบา วิธีที่กระบวนการสื่อสารกัน และหลักการทำงานพร้อมกันแบบ 'ไม่แบ่งปันสิ่งใด' คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “กระบวนการและการส่งข้อความใน Elixir” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กระบวนการและการส่งข้อความใน Elixir
  2. การนำพฤติกรรม GenServer มาใช้
  3. ซูเปอร์ไวเซอร์และโครงสร้างแอปพลิเคชัน
  4. การทำงานพร้อมกันด้วย Task และ Agent
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development