txt: ## scheduler.md
  role Scheduler
  
  Scheme for automatically assigning tasks to threads
  
  ```raku
  role Scheduler {
      has &.uncaught_handler is rw
  }
  ```
  
  A scheduler is a piece of code that determines which resources to use to run which task, and when. This role contains code that will be the same for all kinds of schedulers.
  
  Some operations for example on [`Proc::Async`](/type/Proc/Async), [`Promise`](/type/Promise), [`Supply`](/type/Supply) allow you to specify a scheduler explicitly; they generally expect those schedulers to follow the interface defined by `Scheduler`.
  
  # [Methods](#role_Scheduler "go to top of document")[§](#Methods "direct link")
  
  ## [method uncaught\_handler](#role_Scheduler "go to top of document")[§](#method_uncaught_handler "direct link")
  
  ```raku
  method uncaught_handler() is rw
  ```
  
  RW-Accessor for the handler that is caught for uncaught exceptions from the code that is being scheduled and run.
  
  ## [method cue](#role_Scheduler "go to top of document")[§](#method_cue "direct link")
  
  ```raku
  method cue(&code, Instant :$at, :$in, :$every, :$times = 1; :&catch --> Cancellation)
  ```
  
  Schedules a callable (`&code`) for execution and returns an instantiated [`Cancellation`](/type/Cancellation) object to cancel the scheduling of the code for execution (which is especially important if you specify the `:every ( time )` named parameter). The adverbs control when and how the code is run:
  
  * `$at` can be an [`Instant`](/type/Instant) before which the code won't be run. Alternatively `$in` is the number of seconds (possibly fractional) to wait before running the code. If `$at` is in the past or `$in` is negative, the delay is treated as zero. Implementations may equate to zero very small values (e.g. lower than 0.001s) of `$in` or result of `$at` - [now](/routine/now).
  * If `$every` is specified, it is interpreted as the number of seconds (possibly fractional) to wait before re-executing the code. Implementations may treat too-small values as lowest resolution they support, possibly warning in such situations; e.g. treating `0.0001` as `0.001`.
  * `$times` tells the scheduler how many times to run the code.
  * `&catch` is called with the [`Exception`](/type/Exception) as its sole argument if `&code` dies.
  * If `$at` or `$in` are `Inf`, `&code` will never be run; if `$every` is `Inf`, `&code` will only be run once. If any of the three are `-Inf`, `&code` will be run immediately. If any of the three are `NaN`, an [`X::Scheduler::CueInNaNSeconds`](/type/X/Scheduler/CueInNaNSeconds) exception will be thrown. This only applies to releases 2019.05 and later.
  
  One should call the `cancel` method on the returned [`Cancellation`](/type/Cancellation) object to cancel the (possibly repeated) cueing of the code.
  
  ## [method loads](#role_Scheduler "go to top of document")[§](#method_loads "direct link")
  
  ```raku
  method loads()
  ```
  
  This is a method stub, should be re-implemented when subclassing.
  
  # [Typegraph](# "go to top of document")[§](#typegraphrelations "direct link")
  
  Type relations for `Scheduler`
  
  
  
  raku-type-graph
  
  
  
  Scheduler
  Scheduler
  
  
  Mu
  Mu
  
  
  Any
  Any
  
  
  Any->Mu
  
  
  
  ThreadPoolScheduler
  ThreadPoolScheduler
  
  
  ThreadPoolScheduler->Scheduler
  
  
  
  ThreadPoolScheduler->Any
  
  
  
  CurrentThreadScheduler
  CurrentThreadScheduler
  
  
  CurrentThreadScheduler->Scheduler
  
  
  
  CurrentThreadScheduler->Any
  
  [Expand chart above](/assets/typegraphs/Scheduler.svg)
  