最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - How Can I Ensure Suggestions Appear in the CLI Despite Long Execution Times? - Stack Overflow

programmeradmin3浏览0评论

I am developing a command predictor in C# following this guide. I managed to get the predictions working, but I hit a blocking point. If the execution time of the GetSuggestion() function is too long, the suggestion no longer appears in the command line interface.

    public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContext context, CancellationToken cancellationToken)
{
    string input = context.InputAst.Extent.Text;
    if (string.IsNullOrWhiteSpace(input))
    {
        return default;
    }

    if (input.EndsWith(" "))
    {
        var a = 0;
        for (int i = 0; i < int.MaxValue; i++)
        { 
            a++;
            a--;
        }
            
        return new SuggestionPackage(new List<PredictiveSuggestion>{
        new PredictiveSuggestion(string.Concat(input, a.ToString()))
    });
    }
    
    return new SuggestionPackage(new List<PredictiveSuggestion>{
        new PredictiveSuggestion(string.Concat(input, " example"))
    });
}

While normally testing, I noticed that, when entering the if statement, the suggestion is ignored, while the one normally returned is shown in the CLI.

This is done to simulate a real life scenario, where a 3rd party script would execute instead of that for loop.

My question is: how can I still display the suggestion despite the long execution time?

I am developing a command predictor in C# following this guide. I managed to get the predictions working, but I hit a blocking point. If the execution time of the GetSuggestion() function is too long, the suggestion no longer appears in the command line interface.

    public SuggestionPackage GetSuggestion(PredictionClient client, PredictionContext context, CancellationToken cancellationToken)
{
    string input = context.InputAst.Extent.Text;
    if (string.IsNullOrWhiteSpace(input))
    {
        return default;
    }

    if (input.EndsWith(" "))
    {
        var a = 0;
        for (int i = 0; i < int.MaxValue; i++)
        { 
            a++;
            a--;
        }
            
        return new SuggestionPackage(new List<PredictiveSuggestion>{
        new PredictiveSuggestion(string.Concat(input, a.ToString()))
    });
    }
    
    return new SuggestionPackage(new List<PredictiveSuggestion>{
        new PredictiveSuggestion(string.Concat(input, " example"))
    });
}

While normally testing, I noticed that, when entering the if statement, the suggestion is ignored, while the one normally returned is shown in the CLI.

This is done to simulate a real life scenario, where a 3rd party script would execute instead of that for loop.

My question is: how can I still display the suggestion despite the long execution time?

Share Improve this question asked Mar 31 at 17:34 KudorKudor 696 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There are some unknown bits to your code, for example, I don't know what's inside the PredictiveSuggestion() ctor. Having that said, there are 2 main approaches that 'may' help you to fix this issue:

A. Make the GetSuggestion() async. This will help to offload the work, if it is CPU-heavy to a background thread:

public async Task<SuggestionPackage> GetSuggestionAsync(PredictionClient client, PredictionContext context, CancellationToken cancellationToken)
{
    string input = context.InputAst.Extent.Text;
    if (string.IsNullOrWhiteSpace(input))
    {
        return default;
    }

    if (input.EndsWith(" "))
    {
        var result = await Task.Run(() =>
        {
            int a = 0;
            for (int i = 0; i < 1000; i++) // Reduced iterations for demonstration
            { 
                a++;
                a--;
            }
            return string.Concat(input, a.ToString());
        });

        return new SuggestionPackage(new List<PredictiveSuggestion>
        {
            new PredictiveSuggestion(result)
        });
    }

    return new SuggestionPackage(new List<PredictiveSuggestion>
    {
        new PredictiveSuggestion(string.Concat(input, " example"))
    });
}

B. I don't know if the SuggestionPackage model includes some essential metadata or not, but whenever you want to return a complex collection, it's a good idea to take advantage of C#'s yield keyword to have a non-blocking stream of returned items. Here is an example on how to do this:

public IEnumerable<PredictiveSuggestion> GetSuggestions(PredictionContext context)
{
    string input = context.InputAst.Extent.Text;
    if (string.IsNullOrWhiteSpace(input))
    {
        yield break;
    }

    if (input.EndsWith(" "))
    {
        int a = 0;
        for (int i = 0; i < 1000; i++) // Reduced iterations for demonstration
        {
            a++;
            a--;
            yield return new PredictiveSuggestion(string.Concat(input, a.ToString()));
        }
    }
    else
    {
        yield return new PredictiveSuggestion(string.Concat(input, " example"));
    }
}

You can combine A and B, but I assume B (yield) might work good enough. Cheers! ( ͡~ ͜つ ͡ᵔ )

发布评论

评论列表(0)

  1. 暂无评论