Run tasks in a batch
up vote
0
down vote
favorite
I am trying to understand System.Task
, but I am not sure if what I am doing is correct. My goal is to write a method to process images parallel in a batch.
Is my understanding correct, that in case of batchSize = 3
the method will queue 3 tasks and run all 3 tasks in parallel as soon as Task.WaitAll()
being called ?
Is there are more elegant way if doing it ?
private static void ProcessImages(int batchSize)
{
List<Task> tasks = new List<Task>();
foreach (var image in ImageSource.ReadImages())
{
if(tasks.Count < batchSize)
{
tasks.Add(Task.Run(() => ImageProcessor.ProcessImage(image)));
}
else
{
Task.WaitAll(tasks.ToArray());
tasks.Clear();
}
}
}
c# task task-parallel-library
add a comment |
up vote
0
down vote
favorite
I am trying to understand System.Task
, but I am not sure if what I am doing is correct. My goal is to write a method to process images parallel in a batch.
Is my understanding correct, that in case of batchSize = 3
the method will queue 3 tasks and run all 3 tasks in parallel as soon as Task.WaitAll()
being called ?
Is there are more elegant way if doing it ?
private static void ProcessImages(int batchSize)
{
List<Task> tasks = new List<Task>();
foreach (var image in ImageSource.ReadImages())
{
if(tasks.Count < batchSize)
{
tasks.Add(Task.Run(() => ImageProcessor.ProcessImage(image)));
}
else
{
Task.WaitAll(tasks.ToArray());
tasks.Clear();
}
}
}
c# task task-parallel-library
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to understand System.Task
, but I am not sure if what I am doing is correct. My goal is to write a method to process images parallel in a batch.
Is my understanding correct, that in case of batchSize = 3
the method will queue 3 tasks and run all 3 tasks in parallel as soon as Task.WaitAll()
being called ?
Is there are more elegant way if doing it ?
private static void ProcessImages(int batchSize)
{
List<Task> tasks = new List<Task>();
foreach (var image in ImageSource.ReadImages())
{
if(tasks.Count < batchSize)
{
tasks.Add(Task.Run(() => ImageProcessor.ProcessImage(image)));
}
else
{
Task.WaitAll(tasks.ToArray());
tasks.Clear();
}
}
}
c# task task-parallel-library
I am trying to understand System.Task
, but I am not sure if what I am doing is correct. My goal is to write a method to process images parallel in a batch.
Is my understanding correct, that in case of batchSize = 3
the method will queue 3 tasks and run all 3 tasks in parallel as soon as Task.WaitAll()
being called ?
Is there are more elegant way if doing it ?
private static void ProcessImages(int batchSize)
{
List<Task> tasks = new List<Task>();
foreach (var image in ImageSource.ReadImages())
{
if(tasks.Count < batchSize)
{
tasks.Add(Task.Run(() => ImageProcessor.ProcessImage(image)));
}
else
{
Task.WaitAll(tasks.ToArray());
tasks.Clear();
}
}
}
c# task task-parallel-library
c# task task-parallel-library
asked Nov 11 at 3:40
koryakinp
1,64521036
1,64521036
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
Is my understanding correct, that in case of batchSize = 3 the method
will queue 3 tasks and run all 3 tasks in parallel as soon as
Task.WaitAll() being called ?
While the idea to add tasks to the list and then Task.WaitAll()
these tasks is correct, your code is unfortunatelly buggy and will not work as expected. Specifically, it will NOT execute tasks for images that follows after each batch and also it will NOT execute tasks for images in the last batch, if number of images is not divisible by batchSize+1
.
Is there are more elegant way if doing it ?
Fortunatelly, executing tasks in batches is very common requirement, so .NET already contains methods that makes it much easier. With PLINQ it is as simple as this:
ImageSource.ReadImages()
.AsParallel()
.WithDegreeOfParallelism(batchSize)
.ForAll(image => ImageProcessor.ProcessImage(image));
add a comment |
up vote
1
down vote
Another option is to use Microsoft's Reactive Framework (NuGet "System.Reactive").
Then this works:
ImageSource
.ReadImages()
.ToObservable()
.Select(image => Observable.Start(() => ImageProcessor.ProcessImage(image)))
.Merge(maxConcurrent: 3)
.Wait();
IMHO the Reactive Framework is far more powerful than tasks.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Is my understanding correct, that in case of batchSize = 3 the method
will queue 3 tasks and run all 3 tasks in parallel as soon as
Task.WaitAll() being called ?
While the idea to add tasks to the list and then Task.WaitAll()
these tasks is correct, your code is unfortunatelly buggy and will not work as expected. Specifically, it will NOT execute tasks for images that follows after each batch and also it will NOT execute tasks for images in the last batch, if number of images is not divisible by batchSize+1
.
Is there are more elegant way if doing it ?
Fortunatelly, executing tasks in batches is very common requirement, so .NET already contains methods that makes it much easier. With PLINQ it is as simple as this:
ImageSource.ReadImages()
.AsParallel()
.WithDegreeOfParallelism(batchSize)
.ForAll(image => ImageProcessor.ProcessImage(image));
add a comment |
up vote
3
down vote
accepted
Is my understanding correct, that in case of batchSize = 3 the method
will queue 3 tasks and run all 3 tasks in parallel as soon as
Task.WaitAll() being called ?
While the idea to add tasks to the list and then Task.WaitAll()
these tasks is correct, your code is unfortunatelly buggy and will not work as expected. Specifically, it will NOT execute tasks for images that follows after each batch and also it will NOT execute tasks for images in the last batch, if number of images is not divisible by batchSize+1
.
Is there are more elegant way if doing it ?
Fortunatelly, executing tasks in batches is very common requirement, so .NET already contains methods that makes it much easier. With PLINQ it is as simple as this:
ImageSource.ReadImages()
.AsParallel()
.WithDegreeOfParallelism(batchSize)
.ForAll(image => ImageProcessor.ProcessImage(image));
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Is my understanding correct, that in case of batchSize = 3 the method
will queue 3 tasks and run all 3 tasks in parallel as soon as
Task.WaitAll() being called ?
While the idea to add tasks to the list and then Task.WaitAll()
these tasks is correct, your code is unfortunatelly buggy and will not work as expected. Specifically, it will NOT execute tasks for images that follows after each batch and also it will NOT execute tasks for images in the last batch, if number of images is not divisible by batchSize+1
.
Is there are more elegant way if doing it ?
Fortunatelly, executing tasks in batches is very common requirement, so .NET already contains methods that makes it much easier. With PLINQ it is as simple as this:
ImageSource.ReadImages()
.AsParallel()
.WithDegreeOfParallelism(batchSize)
.ForAll(image => ImageProcessor.ProcessImage(image));
Is my understanding correct, that in case of batchSize = 3 the method
will queue 3 tasks and run all 3 tasks in parallel as soon as
Task.WaitAll() being called ?
While the idea to add tasks to the list and then Task.WaitAll()
these tasks is correct, your code is unfortunatelly buggy and will not work as expected. Specifically, it will NOT execute tasks for images that follows after each batch and also it will NOT execute tasks for images in the last batch, if number of images is not divisible by batchSize+1
.
Is there are more elegant way if doing it ?
Fortunatelly, executing tasks in batches is very common requirement, so .NET already contains methods that makes it much easier. With PLINQ it is as simple as this:
ImageSource.ReadImages()
.AsParallel()
.WithDegreeOfParallelism(batchSize)
.ForAll(image => ImageProcessor.ProcessImage(image));
answered Nov 11 at 5:51
Ňuf
4,81421222
4,81421222
add a comment |
add a comment |
up vote
1
down vote
Another option is to use Microsoft's Reactive Framework (NuGet "System.Reactive").
Then this works:
ImageSource
.ReadImages()
.ToObservable()
.Select(image => Observable.Start(() => ImageProcessor.ProcessImage(image)))
.Merge(maxConcurrent: 3)
.Wait();
IMHO the Reactive Framework is far more powerful than tasks.
add a comment |
up vote
1
down vote
Another option is to use Microsoft's Reactive Framework (NuGet "System.Reactive").
Then this works:
ImageSource
.ReadImages()
.ToObservable()
.Select(image => Observable.Start(() => ImageProcessor.ProcessImage(image)))
.Merge(maxConcurrent: 3)
.Wait();
IMHO the Reactive Framework is far more powerful than tasks.
add a comment |
up vote
1
down vote
up vote
1
down vote
Another option is to use Microsoft's Reactive Framework (NuGet "System.Reactive").
Then this works:
ImageSource
.ReadImages()
.ToObservable()
.Select(image => Observable.Start(() => ImageProcessor.ProcessImage(image)))
.Merge(maxConcurrent: 3)
.Wait();
IMHO the Reactive Framework is far more powerful than tasks.
Another option is to use Microsoft's Reactive Framework (NuGet "System.Reactive").
Then this works:
ImageSource
.ReadImages()
.ToObservable()
.Select(image => Observable.Start(() => ImageProcessor.ProcessImage(image)))
.Merge(maxConcurrent: 3)
.Wait();
IMHO the Reactive Framework is far more powerful than tasks.
answered Nov 12 at 12:32
Enigmativity
74.5k764129
74.5k764129
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245639%2frun-tasks-in-a-batch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown