Skip to content

Async/Concurrency #2

Description

@Calvindd2f

If invocations are sync (runspace.Invoke()), long scripts could block the IoT listener. Suggestion: Wrap in Task.Run() with CancellationToken from Hub method context. For multi-device fleets, add a semaphore (SemaphoreSlim) to throttle concurrent runspaces (e.g., max 2-3 to avoid OOM).

// Pseudo-patch in Executor
private async Task<ExecutionResult> ExecuteAsync(string script, CancellationToken ct)
{
    var semaphore = new SemaphoreSlim(2, 2); // Configurable
    await semaphore.WaitAsync(ct);
    try
    {
        using var runspace = RunspaceFactory.CreateRunspace(host);
        runspace.Open();
        using var pipeline = runspace.CreatePipeline();
        pipeline.Commands.AddScript(script);
        var results = await Task.Run(() => pipeline.InvokeAsync(ct), ct);
        // Serialize & return
    }
    finally { semaphore.Release(); }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions