Best way to limit the number of active Tasks running via the Parallel Task Library

前端 未结 6 1913
春和景丽
春和景丽 2020-12-02 16:54

Consider a queue holding a lot of jobs that need processing. Limitation of queue is can only get 1 job at a time and no way of knowing how many jobs there a

6条回答
  •  广开言路
    2020-12-02 17:36

    I just gave an answer which is very applicable to this question.

    Basically, the TPL Task class is made to schedule CPU-bound work. It is not made for blocking work.

    You are working with a resource that is not CPU: waiting for service replies. This means the TPL will mismange your resource because it assumes CPU boundedness to a certain degree.

    Manage the resources yourself: Start a fixed number of threads or LongRunning tasks (which is basically the same). Decide on the number of threads empirically.

    You can't put unreliable systems into production. For that reason, I recommend #1 but throttled. Don't create as many threads as there are work items. Create as many threads which are needed to saturate the remote service. Write yourself a helper function which spawns N threads and uses them to process M work items. You get totally predictable and reliable results that way.

提交回复
热议问题