start
ErrorsCollection

start

Synthesised documentation from type/Promise type/Supply type/Proc/Async type/Thread

From type/Promise

See Original text in context

method start(Promise:U: &code:$scheduler = $*SCHEDULER --> Promise:D)

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 $p1 = Promise.start({ ;#`( do something here ) });
my $p2 = start { ;#`( do something here ) };

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 { die }sleep ⅓; say "hello"# OUTPUT: «hello␤» 
use v6.d;
start { die }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 { die }sleep ⅓; say "hello"# OUTPUT: «hello␤» 
 
# Catch yourself: 
start { dieCATCH { default { say "caught" } } };
sleep ⅓;
say "hello";
# OUTPUT: «caught␤hello␤» 

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.

From type/Supply

See Original text in context

method start(Supply:D: &startee --> Supply:D)

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.

From type/Proc/Async

See Original text in context

method start(Proc::Async:D: :$scheduler = $*SCHEDULER:$ENV:$cwd = $*CWD --> Promise)

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 $p.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 $p.start;

From type/Thread

See Original text in context

method start(Thread:U: &codeBool :$app_lifetime = FalseStr :$name = '<anon>' --> Thread:D)

Creates, runs and returns a new Thread. Note that it can (and often does) return before the thread's code has finished running.