Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

387
Views
¿Cómo restringir la concurrencia (como `maxConcurrentOperationCount`) con Swift Concurrency?

Estoy tratando de realizar una serie de solicitudes de red y me gustaría limitar la cantidad de tareas simultáneas en el nuevo sistema Swift Concurrency. Con las colas de operaciones, maxConcurrentOperationCount . En Combine, flatMap(maxPublishers:_:) . ¿Cuál es el equivalente en el nuevo sistema Swift Concurrency?

Por ejemplo, no es muy relevante, pero considere:

 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() } }

Eso da como resultado que todas las solicitudes se ejecuten simultáneamente:

ingrese la descripción de la imagen aquí

El hecho de que URLSession no httpMaximumConnectionsPerHost es interesante, pero no es el problema principal aquí. Estoy buscando, de manera más general, cómo restringir el grado de concurrencia en una serie de tareas asincrónicas que se ejecutan en paralelo.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Uno puede insertar una llamada group.next() dentro del bucle después de alcanzar un determinado conteo, por ejemplo:

 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() } }

Eso da como resultado no más de seis a la vez:

ingrese la descripción de la imagen aquí

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!