=================
== The Archive ==
=================

[Elixir] Task (async, await)

|

Introduction

async_stream

1
2
3
4
5
stream =
  ["long string", "longer string", "there are many of these"]
  |> Task.async_stream(fn text -> text |> String.codepoints() |> Enum.count() end)

assert Enum.reduce(stream, 0, fn {:ok, num}, acc -> num + acc end) == 47

unbound async + take

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
result =
  1..100
  |> Stream.take(10)
  |> Task.async_stream(fn i ->
    Process.sleep(100)
    i
  end)
  |> Enum.map(fn {:ok, val} -> val end)
  |> Enum.to_list()

assert result == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

async / await

1
2
task = Task.async(fn -> Enum.sum(0..999_999) end)
assert Task.await(task) == 499_999_500_000

await_many

1
2
3
4
5
6
tasks = [
  Task.async(fn -> 1 + 1 end),
  Task.async(fn -> 2 + 3 end)
]

assert Task.await_many(tasks) == [2, 5]

References

Categories:

Tags: