diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml index 42b8e57..b2b04ac 100644 --- a/.github/workflows/elixir.yml +++ b/.github/workflows/elixir.yml @@ -24,7 +24,7 @@ jobs: uses: erlef/setup-beam@v1 with: - elixir-version: '1.14.3' # Define the elixir version [required] + elixir-version: '1.15.7' # Define the elixir version [required] otp-version: '25.2' # Define the OTP version [required] - name: Install dependencies run: mix deps.get diff --git a/intro_actors/lib/genserver_example.ex b/intro_actors/lib/genserver_example.ex index f8766e0..f7ae33a 100644 --- a/intro_actors/lib/genserver_example.ex +++ b/intro_actors/lib/genserver_example.ex @@ -2,23 +2,34 @@ defmodule Post do use GenServer #Implementación con actores y Genserver... - def start(state, name) do - GenServer.start(__MODULE__, state, name: name) + # funciones de creacion + def start(cantidad_inicial_likes, name) do + GenServer.start(__MODULE__, cantidad_inicial_likes, name: name) end - def start_link(state, name) do - GenServer.start_link(__MODULE__, state, name: name) + def start_link(cantidad_inicial_likes, name) do + GenServer.start_link(__MODULE__, cantidad_inicial_likes, name: name) end + # estado inicial def init(cantidad_inicial_likes) do {:ok, cantidad_inicial_likes} end - def handle_call(:get, from, state) do - IO.puts 'Recibi pedido de cant de likes de #{inspect from}' + ## handle + + ## envio sincronico + def handle_call(:get, from_pid, state) do + IO.puts 'Recibi pedido de cant de likes de #{inspect from_pid}' {:reply, state, state} end + def handle_call(:blah, from_pid, state) do + IO.puts 'Recibi pedido de blah de #{inspect from_pid}' + {:reply, state+1, state} + end + + ## envio asincronico def handle_cast({:like, pid}, state) do IO.puts "Recibi :like de #{inspect pid}" send pid, {:ok, "Recibi tu like"} @@ -26,6 +37,11 @@ defmodule Post do {:noreply, nuevo_estado} end + def handle_cast({:like_n, n}, state) do + nuevo_estado = state + n + {:noreply, nuevo_estado} + end + # --- funciones de uso --- def like(post, pid) do @@ -37,10 +53,11 @@ defmodule Post do end end -#{:ok, post} = Post.start_link(0, :post_principal) + +#{:ok, post} = Post.start(0, :post_principal) #for _ <- 1..1000, do: Post.like(:post_principal) # aumentar_like = fn -> for _ <- 1..1000, do -# Post.like(:post_principal) +# Post.like(:post_principal) # :timer.sleep(50) # end # end diff --git a/intro_actors/lib/intro_actors.ex b/intro_actors/lib/intro_actors.ex index 1a2ebcf..0dbf5c9 100644 --- a/intro_actors/lib/intro_actors.ex +++ b/intro_actors/lib/intro_actors.ex @@ -4,12 +4,18 @@ defmodule IntroActors do spawn(fn -> loop() end) end + def start_link do + spawn_link(fn -> loop() end) + end + def loop() do receive do {_, :camina} -> IO.puts 'Jose empieza a caminar. Proceso #{inspect self()}' {_, :corre} -> IO.puts 'Ni ahi. Proceso #{inspect self()}' {pid, _ } -> send pid, {:error, 'Accion Invalida de #{inspect pid}'} + _ -> IO.puts 'Accion Invalida...' end + loop() end end diff --git a/intro_actors/lib/intro_agent.ex b/intro_actors/lib/intro_agent.ex index 90be23e..55673cf 100644 --- a/intro_actors/lib/intro_agent.ex +++ b/intro_actors/lib/intro_agent.ex @@ -1,6 +1,10 @@ defmodule KV.Agent do use Agent + def start do + Agent.start(fn -> %{} end) + end + def start_link do Agent.start_link(fn -> %{} end) end @@ -15,8 +19,6 @@ defmodule KV.Agent do def put(agent, key, value) do Agent.update(agent, &Map.put(&1, key, value)) end - - end @@ -24,5 +26,5 @@ end # {:ok, #PID<0.166.0>} # iex(9)> KV.Agent.put(pid, :hello, 1) # :ok -# iex(10)> KV.Agent.get(pid, :hello) +# iex(10)> KV.Agent.get(pid, :hello) # 1 diff --git a/intro_actors/lib/intro_consola._ex b/intro_actors/lib/intro_consola._ex index 06afbad..02d65a1 100644 --- a/intro_actors/lib/intro_consola._ex +++ b/intro_actors/lib/intro_consola._ex @@ -26,6 +26,13 @@ iex(6)> flush :bleh :ok +receive do + {:hello, msg} -> msg + {:world, msg} -> 'dont match' +after + 10_000 -> "No paso nada después de 10s" +end + #veamos algo ahora un poco más complejo iex(9)> send self(), {:hello, "world"} {:hello, "world"} @@ -102,7 +109,7 @@ iex(23)> flush #hay secuencialidad? -g = fn(x) -> :timer.sleep(50); IO.puts "#{x}" end +g = fn(x) -> :timer.sleep(100); IO.puts "#{x}" end for n <- 1..10, do: spawn(fn-> g.(n) end) iex(1)> g = fn(x) -> :timer.sleep(50); IO.puts "#{x}" end @@ -212,4 +219,11 @@ iex(3)> :shell_default.i(0,62,0) minor_gcs: 1 ], suspending: [] -] \ No newline at end of file +] + + +spawn fn -> raise "ups" end + +spawn_link fn -> raise "ups" end + +Process.info(jose, :links) \ No newline at end of file diff --git a/intro_actors/lib/intro_task.ex b/intro_actors/lib/intro_task.ex index 113449c..e13278f 100644 --- a/intro_actors/lib/intro_task.ex +++ b/intro_actors/lib/intro_task.ex @@ -8,6 +8,10 @@ defmodule IASC.WriteHaiku do Task.start_link(__MODULE__, :run, [arg]) end + def start_run(arg) do + Task.start(__MODULE__, :run, [arg]) + end + def start_link_read() do Task.start_link(__MODULE__, :read_haiku, []) end @@ -43,4 +47,4 @@ end # {:ok, pid} = IASC.WriteHaiku.start_link_read() #haiku= "Arbol sereno\nque en el bosque protege\na tantos seres" -#{:ok, pid} = IASC.WriteHaiku.start_link_run(haiku) \ No newline at end of file +#{:ok, pid} = IASC.WriteHaiku.start_link_run(haiku) diff --git a/intro_actors/mix.exs b/intro_actors/mix.exs index 0304384..aca4a03 100644 --- a/intro_actors/mix.exs +++ b/intro_actors/mix.exs @@ -4,7 +4,7 @@ defmodule Actors.Mixfile do def project do [app: :intro_actors, version: "0.0.3", - elixir: "~> 1.9", + elixir: "~> 1.15", build_embedded: Mix.env == :prod, start_permanent: Mix.env == :prod, deps: deps()] @@ -14,7 +14,7 @@ defmodule Actors.Mixfile do # # Type `mix help compile.app` for more information def application do - [applications: [:logger]] + [applications: [:logger, :eex, :wx, :observer, :runtime_tools]] end # Dependencies can be Hex packages: