concurrency - Scala ProcessLogger & Future: How can I make sure all line callbacks are called BEFORE the onComplete block? -
import scala.sys.process._ import scala.concurrent._ // (future) import executioncontext.implicits.global // (future) /* run "ls /opt" * outputs 2 lines: * - "x11" * - "local" */ val command = process(seq("ls", "/opt"), none, "lang" -> "") val process = command.run(processlogger(line => { thread.sleep(1000) // slow execution down println(thread.currentthread) println(line) println("") })) future { process.exitvalue // blocks until process exits }.oncomplete { results => println(thread.currentthread) println("process exited: after last line callback finished?") println("done") }
output (order as expected, case?):
thread[thread-47,5,run-main-group-0] line x11 thread[thread-47,5,run-main-group-0] line local thread[forkjoinpool-1-worker-3,5,run-main-group-0] process exited: after last line callback finished? done
it seems o.k. because oncomplete
block outputs text after line-related output over. case? makes sure different threads run 1 after other? can rely on supposed order?
a future object holding value may become available @ point. value result of other computation:
- if computation has not yet completed, future not completed.
- if computation has completed value or exception, future completed.
completion can take 1 of 2 forms:
when future completed value, future completed value.
when future completed exception thrown computation, future failed exception.
the simplest way create future object invoke future method starts asynchronous computation , returns future holding result of computation. result becomes available once future completes.
note future[t] type denotes future objects, whereas future method creates , schedules asynchronous computation, , returns future object completed result of computation.
you can see more implementation details @ github
Comments
Post a Comment