How to call a function with Process no linked and destroy after some period

Problem: I want to upload a file between 1mb to 50mb to S3, but I don’t want to keep user waiting for the Response in the UI, I want to do it async, spawning a process and execute it, after X seconds(or the execution) destroy the process Supervisor

Example: (The library using this is the ex_aws_s3)

defmodule Upload do
  #lets assume that upload is already uploaded using phoenix
  def process_store(upload) do
    {:ok, pid} = Task.Supervisor.start_link()

     Task.Supervisor.async_nolink(pid, fn ->
        {:ok, file_binary} =

        ExAws.S3.put_object("my-bucker", "my-file", file_binary,[{:acl, :bucket_owner_full_control}])
       |> ExAws.request()

What I want is: After process_store/1, ensure that the Task.Supervisor.async_nolink/2 will be executed and kill the supervisor pid without link or await the current process.

1 Like

If you aren’t doing anything to the file that is uploaded by the user wouldn’t it be better to upload it directly to S3 from the users browser?


I’m doing, this is only a snippet of code, an example, The case is not the upload,
I just want to know if is possible I run Task.Supervisor.start_link() and Task.Supervisor.async_nolink
without block current process and stop/kill the supervisor after X seconds or something like that

What happens with I do not use this Supervisor Process, he will stay alive in BEAM forever?

1 Like

I think you want to start the Task supervisor as part of your supervision tree and use dynamically started tasks.

Inside your lib/app/application.ex
children = [

{Task.Supervisor, name: MyApp.TaskSupervisor, shutdown: XXXX}


Task.Supervisor.start_child(MyApp.TaskSupervisor, fn ->
  # Do something

I haven’t used tasks extensively so I might be wrong, but perhaps writing your own genserver that is started dynamically for every file upload would be a better approach has you have more ways to control the upload process (including timeouts)?

1 Like