Codename-404

Codename-404

Elixir function doesn't continue if takes too long

def func do
convert_video.(1280)
IO.puts(“Conversion done, starting to upload”)
end

I have this function, but problem is, if convert_video.(1280) takes too long, IO.puts(“Conversion done, starting to upload”) line does not execute, infact nothing after executes.
However if convert_video.(1280) takes short time, all the codes below executes.

How to make sure function always executes fully?

Marked As Solved

lud

lud

Also what you can do is register your process to give it a name:

def func do
  Process.register(self(), :hello)
  convert_video.(1280)
  IO.puts("Conversion done, starting to upload")
end

Then start your app with iex (like iex -S mix phx.server if using Phoenix) and run a single long conversion (otherwise process registration will clash), and once you see in the OS that it is finished, you can call this in IEx:

pid = Process.whereis(:hello)

This will return a pid, or nil. If it returns nil it means that your function either crashed silently, for some reason, or completed but you could not see the output of IO.puts for some reason (but it was executed as Robert says).

If you get a pid, then your process is still awaiting something, or looping forever somewhere. You can then call this in IEx shell:

Process.info(pid, :current_stacktrace)

This will show what is that process doing at the moment.

For instance if for some reason the process is still awaiting a return from a system command, you may see this:

iex(7)> pid = spawn(fn -> System.cmd("watch", ["sleep", "1000"]) end)
#PID<0.116.0>
iex(8)> Process.info(pid, :current_stacktrace)
{:current_stacktrace,
 [
   {System, :do_port_byte, 3, [file: ~c"lib/system.ex", line: 1118]},
   {System, :do_cmd, 3, [file: ~c"lib/system.ex", line: 1107]},
   {:elixir, :eval_external_handler, 3, [file: ~c"src/elixir.erl", line: 396]}
 ]}

Also Liked

rvirding

rvirding

Creator of Erlang

A function always executes until it is completed.

Codename-404

Codename-404

That was really helpful, Thank you very much ~
So, I found out, the process was returing nil after a while, then I realized it was the phoenix timeout after 60 seconds by default.
So I increased the default timeout behaviour, and now it works. Alhamdulillah ~
Thanks again, May Allah return the favour upon you in a greater way ~

dimitarvp

dimitarvp

Consider marking @lud’s answer as the solution. It helps future readers.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42842 311
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41454 115
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53578 245
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52238 488
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47849 226
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement