How to use channel to communicate with a process

I try to run a function with @span and get some status back on the computation using e.g. a Channel before I get the end-result. The following code already works, but I am new to channels and I am wondering if there is a better way to do this.

@everywhere function myfun(chan)
  put!(chan,"starting")
  sleep(3)
  put!(chan,"still computing")
  sleep(3)
  put!(chan,"almost done")
  return 42;
end

c = Channel{String}(100)

ff = @spawn myfun(c);

status = @fetchfrom ff.where take!(c) # returns "starting"
status = @fetchfrom ff.where take!(c) # returns "still computing"
status = @fetchfrom ff.where take!(c) # returns "almost done"
result = fetch(ff) # returns 42

Ideally I would like to let Julia choose a “free” process for computing the tasks (as opposed to make this choise myself with @spawnat).

1 Like