hacer un ejemplo en vlang del patron "worker pool"
- User
hacer un ejemplo en vlang del patron "worker pool"
Assistant
Aquí tienes un ejemplo en Vlang del patrón "worker pool":
import time struct Task { id int } fn worker(id int, tasks chan Task, done chan int) { for task in tasks { println("Worker $id processing task ${task.id}") // Simular trabajo time.sleep(100 * time.millisecond) } println("Worker $id shutting down") done <- id } fn main() { num_workers := 3 num_tasks := 10 // Crear un canal para las tareas tasks := chan Task{cap: num_tasks} done := chan int{cap: num_workers} // Crear los trabajadores for i in 0..num_workers { go worker(i, tasks, done) } // Agregar tareas al canal for i in 0..num_tasks { tasks <- Task{id: i} } // Cerrar el canal para indicar que no hay más tareas tasks.close() // Esperar a que todos los trabajadores terminen for i in 0..num_workers { worker_id := <-done println("Worker $worker_id has finished") } println("All workers have finished") }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.