Processes / Concurrency

  • As easy as spawn to runs code specified at undefined time each with a process_id

  • Only communication is messages between processes

  • Supervisors often linked to processes and will restart on failure

  • Agentarrow-up-right - Simple wrappers around genserver for state.

  • GenServerarrow-up-right - “Generic servers” (processes) that encapsulate state, provide sync and async calls, support code reloading, and more. For ongoing background work

  • Taskarrow-up-right - Asynchronous units of computation that allow spawning a process and potentially retrieving its result at a later time.

  • DynamicSupervisors - Allow you to dynamically create and restart/supervise agents/genservers like if you need to spin one up per user or event

  • Registryarrow-up-right - Allow strings to map to processes(instead of just atoms)

Spawn

spawn(fn() -> loop(50, 1) end) # loop defined up there
spawn(fn() -> loop(100, 50) end)
spawn(MODULE_NAME, FUNCTION_NAME, [ARGS])#alt

spawn_link(fn() -> :ok end) #spawn_link means if one process exits the other one will too(stops dangling p's)

Message Passing

  • messages are only one way, so if want response need to recieve

  • Messages will be put in process mailbox for future use

Process Investigate

Look at livedashboard or erlang observer or

Process Monitor

Last updated