-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRunParallel.cs
More file actions
109 lines (96 loc) · 2.98 KB
/
Copy pathRunParallel.cs
File metadata and controls
109 lines (96 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.Collections;
using BlocklyNet.Core.Model;
using BlocklyNet.Extensions.Builder;
using BlocklyNet.Scripting;
using BlocklyNet.Scripting.Engine;
using BlocklyNet.Scripting.Generic;
namespace BlocklyNet.Extensions;
/// <summary>
/// Run scripts in parallel.
/// </summary>
[CustomBlock(
"run_script_in_parallel",
"Scripts",
@"{
""message0"": ""RunParallel %1 %2 %3 %4 %5"",
""args0"": [
{
""type"": ""input_dummy""
},
{
""type"": ""field_label_serializable"",
""name"": ""SCRIPTS"",
""text"": ""scripts""
},
{
""type"": ""input_value"",
""name"": ""SCRIPTS"",
""check"": [""Array(run_script_by_name)"", ""Array""]
},
{
""type"": ""field_label_serializable"",
""name"": ""LEADINGSCRIPT"",
""text"": ""leading""
},
{
""type"": ""input_value"",
""name"": ""LEADINGSCRIPT"",
""check"": ""Number""
}
],
""output"": null,
""colour"": 230,
""tooltip"": ""Run a a number of scripts in parallel"",
""helpUrl"": """"
}",
@""
)]
public class RunParallel : Block
{
/// <inheritdoc/>
protected override async Task<object?> EvaluateAsync(Context context)
{
/* Request the script blocks in configuration mode - will not execute on this thread. */
context.ParallelMode++;
try
{
/* Load array of scripts. */
var scripts = await Values.EvaluateAsync<IEnumerable>("SCRIPTS", context);
var leading = await Values.EvaluateOptionalDoubleAsync("LEADINGSCRIPT", context);
/* Request configuration for all scripts - allow empty array elements. */
var configs = scripts.Cast<StartScript>().ToList();
/* Lifetime control. */
var leadingDone = false;
/* Create separate tasks for each script. */
var options = new StartScriptOptions { ShouldStopNow = () => leadingDone };
var tasks = configs.Select(config => context.Engine.RunAsync<GenericResult>(config, options)).ToArray();
/* Install debugging context chain. */
IDebugScriptSite? debugSite = context.Engine as IDebugScriptSite;
var oldContext = debugSite?.CallerContext;
debugSite?.CallerContext = context;
try
{
/* Wait for the leading task to finish. */
if (leading is double)
{
await tasks[((int)leading) - 1];
/* Let other tasks stop as soon as possible. */
leadingDone = true;
}
/* Wait for all other tasks to finish and get results */
var results = await Task.WhenAll(tasks);
/* Report combined results. */
return results.Select(r => r.Result).ToArray();
}
finally
{
debugSite?.CallerContext = context;
}
}
finally
{
/* Switch this execution context back to evaluation mode. */
context.ParallelMode--;
}
}
}