hamiltop / streamz

Elixir Streams and Utilities for Streaming.
24 stars 4 forks source link

Are Tasks Connectable? #21

Open hamiltop opened 10 years ago

hamiltop commented 10 years ago

Should tasks be connectable? Is there another construct that looks similar to a task that should be?

If we provide a default implementation for Connectable that supports Streams, then a connectable task would look exactly like Stream.once(fun)

hamiltop commented 10 years ago

Tasks can be connectable if we define a task as follows:

  1. Function to execute on a new process
  2. Connect to another process and send that process a message with the result

The current tasks fit into this idea where #2 is specified (or implied) at launch. The alternative is to launch the task without #2 specified and upon completion the task will wait for connection to a receiving process.

josevalim commented 10 years ago

Tasks can also be connectable if we make them follow the same message pattern (probably the notify one) as the GenEvent messages.

hamiltop commented 10 years ago

Would we allow changing the target of an already launched task? That's what being connectable means to me:

event = GenEvent.start_link
stream = GenEvent.stream(event)
spawn_link fn ->
  stream |> Enum.each &IO.puts/1
end
...
task = Task.async( fn -> "hi" end )
Connectable.connect(task, event)

Should result in "hi" being printed to the screen.

josevalim commented 10 years ago

Ah, I see what you mean. That would be tricky as the task would likely be already done and sent to the caller. We could change how tasks work but that would increase their overhead. Interesting enough, I was thinking the connectable protocol was always about connecting to self (self would always be the second argument). GenEvent is just more flexible.

hamiltop commented 10 years ago

We could have the Task process block until connected.