Spawn: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
No edit summary
m (Text replacement - "</source>" to "</syntaxhighlight>")
Line 3: Line 3:
== Usage ==
== Usage ==


Spawn is used to run background tasks, for example for monitoring or handling incoming requests. Since the thread number is returned, <source lang=apl inline>⎕TSYNC</source> ("Thread Synchronise") is provided to retrieve the result, given the thread number. If the result is ready before <source lang=apl inline>⎕TSYNC</source> is used, it prints to the [[session]] if applicable.
Spawn is used to run background tasks, for example for monitoring or handling incoming requests. Since the thread number is returned, <source lang=apl inline>⎕TSYNC</syntaxhighlight> ("Thread Synchronise") is provided to retrieve the result, given the thread number. If the result is ready before <source lang=apl inline>⎕TSYNC</syntaxhighlight> is used, it prints to the [[session]] if applicable.


Spawn is often used in conjunction with the [[Each]] operator (<source lang=apl inline>¨</source>) to launch multiple threads in parallel.
Spawn is often used in conjunction with the [[Each]] operator (<source lang=apl inline>¨</syntaxhighlight>) to launch multiple threads in parallel.


As Spawn is an operator, it cannot be directly used with a [[niladic function]], since a niladic function is called immediately when its name is referenced. As a workaround, the niladic function can be wrapped in a [[dfn]] and called with a dummy argument: <source lang=apl inline>{nilFun}&⍬</source>
As Spawn is an operator, it cannot be directly used with a [[niladic function]], since a niladic function is called immediately when its name is referenced. As a workaround, the niladic function can be wrapped in a [[dfn]] and called with a dummy argument: <source lang=apl inline>{nilFun}&⍬</syntaxhighlight>


== Examples ==
== Examples ==
Line 16: Line 16:
1
1
0.25
0.25
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
The effect of running code in the background can be observed when a measurable amount of time elapses. In the first expression below, get the current time (in milliseconds) and then [[delay]] (<source lang=apl inline>⎕DL</source>) for each of 1, 2, and 3 seconds, eventually computing the elapsed time, which is slightly more than 1+2+3 seconds (6000 milliseconds). In the second expression, the delaying threads are launched, but we don't await their completion before computing the elapsed time. In the final expression, we synchronise the parallel running delays, giving us a total elapsed time only slightly longer than the longest delay.
The effect of running code in the background can be observed when a measurable amount of time elapses. In the first expression below, get the current time (in milliseconds) and then [[delay]] (<source lang=apl inline>⎕DL</syntaxhighlight>) for each of 1, 2, and 3 seconds, eventually computing the elapsed time, which is slightly more than 1+2+3 seconds (6000 milliseconds). In the second expression, the delaying threads are launched, but we don't await their completion before computing the elapsed time. In the final expression, we synchronise the parallel running delays, giving us a total elapsed time only slightly longer than the longest delay.
<source lang=apl>
<source lang=apl>
       t←⎕AI[3] ⋄ ⎕DL¨⍳3 ⋄ ⎕AI[3]-t
       t←⎕AI[3] ⋄ ⎕DL¨⍳3 ⋄ ⎕AI[3]-t
Line 26: Line 26:
       t←⎕AI[3] ⋄ ⎕TSYNC ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t
       t←⎕AI[3] ⋄ ⎕TSYNC ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t
3046
3046
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]]}}
== External links ==
== External links ==

Revision as of 10:34, 11 September 2022

&

Spawn (&) is a monadic operator which applies its operand in a new wikipedia:green thread. Rather that returning the result of its operand, Spawn returns a shy thread number. This operator is specific to Dyalog APL.

Usage

Spawn is used to run background tasks, for example for monitoring or handling incoming requests. Since the thread number is returned, <source lang=apl inline>⎕TSYNC</syntaxhighlight> ("Thread Synchronise") is provided to retrieve the result, given the thread number. If the result is ready before <source lang=apl inline>⎕TSYNC</syntaxhighlight> is used, it prints to the session if applicable.

Spawn is often used in conjunction with the Each operator (<source lang=apl inline>¨</syntaxhighlight>) to launch multiple threads in parallel.

As Spawn is an operator, it cannot be directly used with a niladic function, since a niladic function is called immediately when its name is referenced. As a workaround, the niladic function can be wrapped in a dfn and called with a dummy argument: <source lang=apl inline>{nilFun}&⍬</syntaxhighlight>

Examples

<source lang=apl>

     ÷&4         ⍝ Reciprocal in background

0.25

     ⎕←÷&4       ⍝ Show thread number; result prints afterwards

1 0.25 </syntaxhighlight>

Works in: Dyalog APL

The effect of running code in the background can be observed when a measurable amount of time elapses. In the first expression below, get the current time (in milliseconds) and then delay (<source lang=apl inline>⎕DL</syntaxhighlight>) for each of 1, 2, and 3 seconds, eventually computing the elapsed time, which is slightly more than 1+2+3 seconds (6000 milliseconds). In the second expression, the delaying threads are launched, but we don't await their completion before computing the elapsed time. In the final expression, we synchronise the parallel running delays, giving us a total elapsed time only slightly longer than the longest delay. <source lang=apl>

     t←⎕AI[3] ⋄ ⎕DL¨⍳3 ⋄ ⎕AI[3]-t

6138

     t←⎕AI[3] ⋄ ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t

0

     t←⎕AI[3] ⋄ ⎕TSYNC ⎕DL&¨⍳3 ⋄ ⎕AI[3]-t

3046 </syntaxhighlight>

Works in: Dyalog APL

External links

Lessons

Documentation


APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace