Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Sichere ProgrammierungBreeze TTS 2 vs ElevenLabs: Open Source TTS Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungAgentic AI vs Generative AI: The 2026 Verdict(23.09.2026 um 05:44 Uhr)
Sichere ProgrammierungI made my agent prove every quote against the source document(23.09.2026 um 05:45 Uhr)
Sichere Programmierung8mb.video Alternative: Skip the Line, Skip the Upsell(23.09.2026 um 05:47 Uhr)
Sichere ProgrammierungBuilding a GTA 6 JSON API for entities and current status(23.09.2026 um 05:52 Uhr)
Sichere ProgrammierungEvery filter needs a documented exception(23.09.2026 um 06:01 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

Elixir Agent module - A simple way to sharing data between processes without implement our process or GenServer

Intro For newbies who just jumped to Elixir, it's hard for fully understanding Elixir process (also GenServer). For easy to work with process, Elixir provides Agent module to support sharing data (state) between two or more processes. …

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!




Intro



For newbies who just jumped to Elixir, it's hard for fully understanding Elixir process (also GenServer). For easy to work with process, Elixir provides Agent module to support sharing data (state) between two or more processes.






Explain



In Elixir, every single line of code is run in a process and because Elixir is functional programming then we don't have global variable (of course, we have a big benefit for this is much more less side effect from avoid using global variable).



To share data between functions we use process dictionary or pass variable as param.



But for between different processes we need more effort to share data (if not used Ets table & :persistent_term - a limited way). We need add code to send & receive a message, also we need to handle state to store global variable (can use process dictionary).



For much more convenient to sharing data between process we can use Agent to hold global data (state) to share between processes or simple between functions in a process.






Implement a Agent



We can implement a module using Agent or directly (less convenient).




defmodule AutoId do
use Agent

def start_link(initial_id) do
Agent.start_link(fn -> initial_value end, name: __MODULE__)
end

def last_id do
Agent.get(__MODULE__, & &1)
end

def set_id(new_id) do
Agent.update(__MODULE__, &(&1 + new_id))
end

def new_id do
Agent.get_and_update(__MODULE__, fn id -> {id, id + 1} end)
end
end






In this example, we make a module to sharing a way to get unique id for all processes in system by call AudoId.new_id().

We can add to a application supervisor or manual start by call AutoId.start_link(0) before using (remember Agent process will crash if our process is crashed).



To add a application supervisor (or our supervisor) we add code like:




children = [
{AutoId, 0}
]

Supervisor.start_link(children, strategy: :one_for_one)






For start manual AutoId process just add code like:




AutoId.start_link(1_000)






And somewhere we can use by call AutoId functions like:




my_id = AutoId.new_id()

#...

# reset id
AutoId.set_id(1_000_000)

other_id = AutoId.new_id()






Agent is simple enough to use and we don't need implement GenServer or write new process for sharing data.



Agent is process then it can be handover or overload, for in case we need get/set data (state) in limited time we can use timeout param (default is 5_000 ms).



Agent also provides config for child_spec for supervisor like GenServer & support distributed, hot code swapping for more use cases.

Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten Elixir Agent module - A simple way to sharing data between processes without implement our process or GenServer

Thematisch verwandte Begriffe: Elixir, Agent, module, simple · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-18163 | IBM Financial Transaction Manager (FTM) for RedHat OpenShift could allow…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick