method start
Documentation for method start
assembled from the following types:
class Thread
From Thread
(Thread) method start
method start(Thread: , Bool : = False, Str : = '<anon>' --> Thread)
Creates, runs and returns a new Thread
. Note that it can (and often does) return before the thread's code has finished running.
class Promise
From Promise
(Promise) method start
method start(Promise: , : = --> Promise)
Creates a new Promise that runs the given code object. The promise will be kept when the code terminates normally, or broken if it throws an exception. The return value or exception can be inspected with the result
method.
The scheduler that handles this promise can be passed as a named argument.
There is also a statement prefix start
that provides syntactic sugar for this method:
# these two are equivalent:my = Promise.start();my = start ;
As of the 6.d version of the language, start
statement prefix used in sink context will automatically attach an exceptions handler. If an exception occurs in the given code, it will be printed and the program will then exit, like if it were thrown without any start
statement prefixes involved.
use v6.c;start ; sleep ⅓; say "hello"; # OUTPUT: «hello»
use v6.d;start ; sleep ⅓; say "hello";# OUTPUT:# Unhandled exception in code scheduled on thread 4# Died# in block at -e line 1
If you wish to avoid this behavior, use start
in non-sink context or catch the exception yourself:
# Don't sink it:my $ = start ; sleep ⅓; say "hello"; # OUTPUT: «hello»# Catch yourself:start ;sleep ⅓;say "hello";# OUTPUT: «caughthello»
This behavior exists only syntactically, by using an alternate .sink
method for Promise objects created by start
blocks in sink context, thus simply sinking a Promise object that was created by other means won't trigger this behavior.
class Supply
From Supply
(Supply) method start
method start(Supply: --> Supply)
Creates a supply of supplies. For each value in the original supply, the code object is scheduled on another thread, and returns a supply either of a single value (if the code succeeds), or one that quits without a value (if the code fails).
This is useful for asynchronously starting work that you don't block on.
Use migrate
to join the values into a single supply again.
class Proc::Async
From Proc::Async
(Proc::Async) method start
method start(Proc::Async: : = , :, : = )
Initiates spawning of the external program. Returns a Promise that will be kept with a Proc object once the external program exits or broken if the program cannot be started. Optionally, you can use a scheduler instead of the default $*SCHEDULER
, or change the environment the process is going to run in via the named argument :$ENV
or the directory via the named argument :$cwd
.
If start
is called on a Proc::Async object on which it has already been called before, an exception of type X::Proc::Async::AlreadyStarted is thrown.
Note: If you wish to await
the Promise and discard its result, using
try await .start;
will throw if the program exited with non-zero status, as the Proc
returned as the result of the Promise throws when sunk and in this case it will get sunk outside the try
. To avoid that, sink it yourself inside the try
:
try sink await .start;