Working with iex I was successful in getting result back from recursive receive of Python script. As below,
Interactive Elixir (1.14.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule Flush do
...(1)> def do_flush() do
...(1)> receive do
...(1)> msg ->
...(1)> IO.inspect(msg)
...(1)> do_flush()
...(1)> after
...(1)> 0 -> :ok
...(1)> end
...(1)> end
...(1)> end
{:module, Flush,
<<70, 79, 82, 49, 0, 0, 5, 216, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 193,
0, 0, 0, 20, 12, 69, 108, 105, 120, 105, 114, 46, 70, 108, 117, 115, 104, 8,
95, 95, 105, 110, 102, 111, 95, 95, 10, ...>>, {:do_flush, 0}}
iex(2)> temp = 16
16
iex(3)> press = 1500
1500
iex(4)> port = Port.open({:spawn, "python3 /Users/gani/Desktop/test.py #{temp} #{press}"}, [:binary])
#Port<0.5>
iex(5)> Flush.do_flush()
{#Port<0.5>, {:data, "1945.335612109853\n"}}
:ok
But the same doesn’t work in Livebook. When I do
Flush.do_flush()
It hangs till I click on “stop”. Why this weird behavior?
However, this works:
receive do
{^port, {:data, result}} ->
IO.puts("Elixir got: #{inspect(result)}")
end
I would like help on:
- why doesn’t the function
Flush.do_flush()
work in Livebook but works in iex, - but more importantly, (because the 2nd version works ok), how do I capture value from :data into a variable.
I’m (still) a newbie, so would appreciate help.
Thanks,
Gani -