10 May 2024 |
| @deyv15:matrix.org left the room. | 17:04:17 |
11 May 2024 |
| @maulik1:matrix.org left the room. | 11:02:44 |
| @skogsbrus:matrix.org left the room. | 17:03:57 |
12 May 2024 |
| @ludwig_nagelhus:matrix.org left the room. | 07:32:43 |
14 May 2024 |
| @martinn404:mozilla.org left the room. | 02:24:19 |
15 May 2024 |
| @hoiitghwtadxhxquj:matrix.org joined the room. | 21:47:41 |
| @hoiitghwtadxhxquj:matrix.org joined the room. | 21:47:42 |
| @hoiitghwtadxhxquj:matrix.org left the room. | 21:47:44 |
19 May 2024 |
b00tr | Hi, I'm tyring to figure out how to create a background task in LiveBook that repeats every second. I have this module ```elixir defmodule Period do use Task
def init(args) do IO.puts("initing") args = Keyword.merge(args, parent: self()) do_run(args) {:ok, args} end
def handle_info(:loop, args) do IO.puts("handled") # do_run(args) {:noreply, args} end
def do_run([every: time, run: f, parent: parent] = args) do Task.async(fn -> apply(f, []) end) Process.send_after(parent, {:loop, args}, time) end end
| 04:04:47 |
b00tr | * Hi, I'm tyring to figure out how to create a background task in LiveBook that repeats every second. I have this module
defmodule Period do
use Task
def init(args) do
IO.puts("initing")
args = Keyword.merge(args, parent: self())
do\_run(args)
{:ok, args}
end
def handle\_info(:loop, args) do
IO.puts("handled")
# do\_run(args)
{:noreply, args}
end
def do\_run(\[every: time, run: f, parent: parent\] = args) do
Task.async(fn -> apply(f, \[\]) end)
Process.send\_after(parent, {:loop, args}, time)
end
| 04:05:17 |
b00tr | * Hi, I'm tyring to figure out how to create a background task in LiveBook that repeats every second. I have this module
defmodule Period do
use Task
def init(args) do
IO.puts("initing")
args = Keyword.merge(args, parent: self())
do\_run(args)
{:ok, args}
end
def handle\_info(:loop, args) do
IO.puts("handled")
# do\_run(args)
{:noreply, args}
end
def do\_run(\[every: time, run: f, parent: parent\] = args) do
Task.async(fn -> apply(f, \[\]) end)
Process.send\_after(parent, {:loop, args}, time)
end
end
| 04:05:29 |
b00tr | * Hi, I'm tyring to figure out how to create a background task in LiveBook that repeats every second. I have this module
defmodule Period do
use Task
def init(args) do
IO.puts("initing")
args = Keyword.merge(args, parent: self())
do_run(args)
{:ok, args}
end
def handle_info(:loop, args) do
IO.puts("handled")
# do_run(args)
{:noreply, args}
end
def do_run([every: time, run: f, parent: parent] = args) do
IO.puts("runing")
Task.async(fn -> apply(f, []) end)
Process.send_after(parent, {:loop, args}, time)
end
end
| 04:07:17 |
b00tr | I'm spawning it like this:
Task.start(fn ->
Period.init(
every: 1_000,
run: fn -> IO.puts("all is good") end
)
end)
| 04:08:42 |
b00tr | Any idea how to make it work please? | 04:17:40 |
maddogx | Isn't Process.send_after() made for something like what you want? | 05:08:24 |
b00tr | In reply to @maddogx:matrix.org Isn't Process.send_after() made for something like what you want? Yeah Im using it. i tried it with genserver but it felt way too complicated and hacky with more code and boilerplate | 10:02:21 |
b00tr | But still I need a Supervisor and Livebook needs a special handling I beleive | 10:03:23 |
b00tr | This works as expected:
defmodule Period do
use GenServer
def start_link(args) do
GenServer.start_link(__MODULE__, args, [])
end
def init(args) do
do_run(args)
{:ok, args}
end
# Self retrigger
def handle_info(:loop, args) do
do_run(args)
{:noreply, args}
end
# Handle Task.async events
def handle_info({_ref, _result}, state) do
{:noreply, state}
end
def handle_info({:DOWN, _ref, :process, _pid, _reason}, state) do
{:noreply, state}
end
defp do_run([every: time, run: fun]) do
_task = Task.start(fn -> apply(fun, []) end)
Process.send_after(self(), :loop, time)
end
end
| 11:56:44 |
b00tr | But how do you add a timeout for the Task in the do_run? | 11:57:12 |
b00tr | * This works as expected:
defmodule Period do
use GenServer
def start_link(args) do
GenServer.start_link(__MODULE__, args, [])
end
def init(args) do
do_run(args)
{:ok, args}
end
# Self retrigger
def handle_info(:loop, args) do
do_run(args)
{:noreply, args}
end
# Handle Task.async events
def handle_info({_ref, _result}, state) do
{:noreply, state}
end
def handle_info({:DOWN, _ref, :process, _pid, _reason}, state) do
{:noreply, state}
end
defp do_run([every: time, run: fun]) do
_task = Task.start(fn -> apply(fun, []) end)
Process.send_after(self(), :loop, time)
end
end
{:ok, child} =
Kino.start_child({
Period,
every: 1_000,
run: fn ->
IO.puts("all is good")
Process.sleep(500)
IO.puts("done")
end
})
Process.sleep(3_000)
Kino.terminate_child(child)
| 11:57:55 |
b00tr | Isn't there a library for that already? | 12:02:23 |
b00tr | This seems to do the job.. But the docs are cryptic https://hexdocs.pm/parent/Periodic.html#content | 14:50:50 |
20 May 2024 |
b00tr | Why are elixir people choosing slack to communicate.. wtf... | 02:15:17 |
munksgaard | There are plenty of people here, on elixir and on elixirforums | 05:53:13 |
munksgaard | s/on elixir/on irc/ | 05:53:28 |
b00tr | here is ~700 people. on slack ~30k 😵💫 | 11:25:24 |
munksgaard | Do you need 30k people to talk to? | 11:28:30 |
b00tr | sometimes :D | 11:32:32 |
Nicd | 90% of the accounts on Slack are inactive leftovers | 15:44:01 |
21 May 2024 |
maddogx | In reply to @b00tr:matrix.org But how do you add a timeout for the Task in the do_run? There has to be some way to provide a child spec to Kino.start_child/1, which would allow you to pass that as initial state to the GenServer. | 02:42:35 |