[Proposal] Add async functions definitions

38 views
Skip to first unread message

Pedro Medeiros

unread,
Jan 23, 2018, 10:01:06 AM1/23/18
to elixir-l...@googlegroups.com
Hi,

I'm writing this proposal based on what I've been doing with Python/Javascript. A way ppl on those communities are doing asynchronous call nowadays is by declaring an async function and if there is a need to wait for its result calls the function await. Once we have the module Task that is capable of handling mostly user cases about async/await feature on those languages. I think we can add async to the function definition to implement 

defmodule Foo do
  async def bar(x)
    # code
  end
end


And on the code by calling Foo.bar/1 we return a Task. And by calling Task.async Foo.foo(1) the result can be returned.

the main benefits on having an approach like that is basically code readability when it is necessary to run a single function as a separate process. today that can be done with something like that

task = Task.async(fn ->
  Foo.bar(:arg)
end)
Task.await(task)

or

task = Task.async(Foo, :bar, [:arg])
Task.await(task)

Also another benefit is having a syntax more similar to other languages. This can make Elixir more welcoming for people from other communities such as python or javascript.
--
Pedro Medeiros
----------------------------------
Cel: +55 (21) 9914-86898
Email: pedr...@gmail.com

Beautiful is better than ugly,
Explicit is better than implicit,
Simple is better than complex,
Complex is better than complicated.

The Zen of Python, by Tim Peters

Louis Pilfold

unread,
Jan 23, 2018, 11:40:55 AM1/23/18
to elixir-l...@googlegroups.com
Hi Pedro

I would worry that this would result in this use of the Task module becoming the de-facto way to do work concurrently in Elixir. I think that the majority of the time there's a better solution, likely on that involves supervision.

I think the need for a shorthand for performing work concurrently is not needed in the same way as in Python etc as it's OK to perform IO that blocks your thread as only one request/item of work is being served by that thread.

Cheers,
Louis

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAJbPmJM_TAtuV5b39ZaC1gd8njtHCyk3xe2_%2BfdgfwhOcA-L7Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Wojtek Mach

unread,
Jan 23, 2018, 2:38:43 PM1/23/18
to elixir-lang-core
> Also another benefit is having a syntax more similar to other languages. This can make Elixir more welcoming for people from other communities such as python or javascript.

Adding syntax and APIs that feel familiar is one way to potentially make it easy for people to learn Elixir. It doesn't seem like a scalable approach however because it'd likely result in bloat of the language and/or stdlib.

Another way is to prepare learning materials, perhaps a blog post "Python/JavaScript async in Elixir", where functions like Task.async,await, Task.Supervisor.async_nolink, Task.async_stream and spawn are discussed.
One could even show that with macros we can implement "async" keyword pretty easily:

defmodule DefAsync do
  defmacro defasync
(call, do: block) do
    quote
do
     
def unquote(call) do
       
Task.async(fn -> unquote(block) end)
     
end
   
end
 
end
end


defmodule
Foo do
 
import DefAsync

  defasync delayed_foo
(value) do
   
:timer.sleep(1_000)
    IO
.inspect value
 
end
end


Foo.delayed_foo(42) |> Task.await()

Pedro Medeiros

unread,
Jan 23, 2018, 2:54:03 PM1/23/18
to elixir-l...@googlegroups.com
Thanks for the response people.

I knwon using tasks always is not the best way to go, but I do fell we can make somehow a better abstraction for processes only with what the language have.

I’m gonna write something down later, thanks for the advice Wojtek.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Pedro Henrique de Souza Medeiros

Ben Wilson

unread,
Jan 23, 2018, 4:25:17 PM1/23/18
to elixir-lang-core
An important thing to note is that in Elixir it's generally the caller of a function that determines whether they  want sync or async behaviour, not the callee. Generally speaking you simply write functions that take input and return output. If a given caller of that function wants that to happen async then it can use Task or some other construct for doing so. If another caller wants to do it sync then they just call the function.

Throughout all of this though all the processes are async with respect to one another already, and this is enforced by the VM and requires no special language handling.
Reply all
Reply to author
Forward
0 new messages