GithubHelp home page GithubHelp logo

Comments (6)

davidfowl avatar davidfowl commented on August 20, 2024

We should show the usage of Task.Factory.StartNew as well.

from aspnetcorediagnosticscenarios.

alefranz avatar alefranz commented on August 20, 2024

My interpretation of that guidance is that given StartNewTask.Factory.StartNew is under the hood creating a background thread, you are better off doing it directly so that you have to explicitly decide how to handle the scenario when you are running on a platform that doesn't support starting new threads, instead of having your application silently behaving unexpectedly.

https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/ThreadPoolTaskScheduler.cs#L45-L51

Was this the intention?

from aspnetcorediagnosticscenarios.

davidfowl avatar davidfowl commented on August 20, 2024

Yes

from aspnetcorediagnosticscenarios.

mosdav avatar mosdav commented on August 20, 2024

Great writing. So in light of the advice to go w/ good-old plain Thread for long running "flows", is there a use case at all to use async code in do-forever/consumer like Tasks ? For example, is it "OK" to use Stephen's Channel inside Task.Run or should we go w/ smth like BlockingQueue and avoid async all together ?
IMO, there is an ambiguity here, we should address

from aspnetcorediagnosticscenarios.

LBensman avatar LBensman commented on August 20, 2024

Instead, spawn a new thread manually to do long running blocking work.

I'm not sure if I completely agree with this advice, in that it's not quite functionally the equivalent of .LongRunning option. The example that is given is indeed a good one for when to use own thread instead of .LongRunning. But allow me a different example where use of .LongRunning would be well justified (IMHO) over own thread:

var citiesPointsOfInterest = new [] {
    LoadDataset(NewYork).ToArray(),  // 250 locations; .ToArray() <-- it's in memory so no task discontinuation on await IO.
    LoadDataset(Paris).ToArray(),  // 400 locations;
    LoadDataset(Moscow).ToArray(),  // 100 locations; 
};

var bestPathThroughCitiesTasks = citiesPointsOfInterest.Select(city =>
    Task.Factory.StartNew(ComputeBestPathThroughPointsOfInterest,  // Oh that wonderful The Traveling Salesman Problem...
        city, TaskCreationOptions.LongRunning // LongRunning here would create new thread so as not to pressure thread pool.
    )).ToArray();

var firstDoneTask = await Task.WhenAny(bestPathThroughCitiesTasks);

SendUserFirstResultToThinkAbout(firstDoneTask.Result);

await Task.WhenAll(bestPathThroughCitiesTasks);

LetThemHaveItAll(bestPathThroughCitiesTasks.Select(t => t.Result));

The point of the above example is that it uses flexibility, expressiveness and ecosystem of TPL where use of own threads, while can certainly be done, would not be nearly as convenient and simple. Such code would require a mechanism where a thread notifies main code that result is ready, main code has to manage threads and listen for completions so as to know where to pick up first result, join thread back, then wait for the rest. Again, can be done, but far less convenient.

And yet the above will run on separate threads doing long computations while perfectly protecting the thread pool and avoiding the problem described in the advice.

I'd suggest amending the advice to make it clear when to use .LongRunning and when to use own thread, as both are valid in their own specific scenarios.

P.S. I think this confusion comes from people understanding "LongRunning" as wallclock time, rather than long contiguous block of operation that won't result in multiple subtasks reducing the computation a bunch of "ShortRunning" computations. If that understanding is there, then when to use .LongRunning becomes clearer.

from aspnetcorediagnosticscenarios.

davidfowl avatar davidfowl commented on August 20, 2024

Feel free to send a PR πŸ˜„ , I don't disagree.

from aspnetcorediagnosticscenarios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.