Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

54
Vistas
SemaphoreSlim vs Parallel Processing for API post calls in .NET

I have a chron job which calls a database table and gets about half a million records returned. I need to loop through all of that data, and send API post's to a third party API. In general, this works fine, but the processing time is forever (10 hours). I need a way to speed it up. I've been trying to use a list of Task with SemaphoreSlim, but running into issues (it doesn't like that my api call returns a Task). I'm wondering if anyone has a solution to this that won't destroy the VM's memory?

Current code looks something like:

foreach(var data in dataList)
{
  try 
  {
    var response = await _apiService.PostData(data);
    _logger.Trace(response.Message);
  } catch//
}

But I'm trying to do this and getting the syntax wrong:

var tasks = new List<Task<DataObj>>();
var throttler = new SemaphoreSlim(10);
foreach(var data in dataList)
{
  await throttler.WaitAsync();
  tasks.Add(Task.Run(async () => {
    try
    {
      var response = await _apiService.PostData(data);
        _logger.Trace(response.Message);
    }
    finally
    {
      throttler.Release();
    }
  }));
}
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Your list is of type Task<DataObj>, but your async lambda doesn't return anything, so its return type is Task. To fix the syntax, just return the value:

var response = await _apiService.PostData(data);
_logger.Trace(response.Message);
return response;

As others have noted in the comments, I also recommend not using Task.Run here. A local async method would work fine:

var tasks = new List<Task<DataObj>>();
var throttler = new SemaphoreSlim(10);
foreach(var data in dataList)
{
  tasks.Add(ThrottledPostData(data));
}
var results = await Task.WhenAll(tasks);

async Task<DataObj> ThrottledPostData(Data data)
{
  await throttler.WaitAsync();
  try
  {
    var response = await _apiService.PostData(data);
    _logger.Trace(response.Message);
    return response;
  }
  finally
  {
    throttler.Release();
  }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda