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

379
Vistas
How to constrain concurrency (like `maxConcurrentOperationCount`) with Swift Concurrency?

I am trying to perform a series of network requests and would like to limit the number of concurrent tasks in the new Swift Concurrency system. With operation queues we would use maxConcurrentOperationCount. In Combine, flatMap(maxPublishers:_:). What is the equivalent in the new Swift Concurrency system?

E.g., it is not terribly relevant, but consider:

func download() async throws {
    try await withThrowingTaskGroup(of: Void.self) { group in
        for i in 0..<20 {
            let source = sourceUrl(for: i)
            let destination = destinationUrl(for: i)

            group.addTask {
                let (url, _) = try await self.session.download(from: source)
                try? FileManager.default.removeItem(at: destination)
                try FileManager.default.moveItem(at: url, to: destination)
            }
        }

        try await group.waitForAll()
    }
}

That results in all the requests running concurrently:

enter image description here

The fact that URLSession is not honoring httpMaximumConnectionsPerHost is interesting, but not the salient issue here. I am looking for, more generally, how to constrain the degree of concurrency in a series of asynchronous tasks running in parallel.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

One can insert a group.next() call inside the loop after reaching a certain count, e.g.:

func download() async throws {
    try await withThrowingTaskGroup(of: Void.self) { group in
        for i in 0..<20 {
            let source = sourceUrl(for: i)
            let destination = destinationUrl(for: i)

            if i >= 6 {                                                     // max of six at a time
                try await group.next()
            }

            group.addTask {
                let (url, _) = try await self.session.download(from: source)
                try? FileManager.default.removeItem(at: destination)
                try FileManager.default.moveItem(at: url, to: destination)
            }
        }

        try await group.waitForAll()
    }
}

That results in no more than six at a time:

enter image description here

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