Skip to content

This Go program efficiently processes tasks using goroutines, channels, and sync.WaitGroup, enabling concurrent execution and synchronization. Ideal for job queues and scalable task processing. πŸš€

Notifications You must be signed in to change notification settings

axah710/Worker-Pool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Go Task Processing Workflow

This Go program demonstrates a simple task processing workflow using goroutines and channels. It initializes a task queue, creates multiple worker goroutines to process tasks concurrently, sends a specified number of tasks to the workers, and waits for all tasks to be completed before printing a confirmation message. The use of a WaitGroup ensures that the main program waits for all workers to finish processing before exiting.


✨ Features

  • ⚑ Utilizes goroutines for concurrent task processing.
  • πŸ“Œ Implements channels for efficient task distribution.
  • πŸ”„ Uses sync.WaitGroup to synchronize worker completion.
  • ⏳ Simulates real-world task processing with time.Sleep.

πŸ“œ Code Overview

package main

import (
	"fmt"
	"sync"
	"time"
)

func worker(workerId int, tasksChannel chan int, waitGroup *sync.WaitGroup) {
	defer waitGroup.Done()
	for taskId := range tasksChannel {
		executeTask(workerId, taskId)
	}
}

func executeTask(workerId int, taskId int) {
	fmt.Printf("Worker %d processing task %d\n", workerId, taskId)
	time.Sleep(time.Second)
}

func main() {
	const totalWorkers = 3
	const totalRequestsAllowed = 10
	tasksChannel := make(chan int, totalRequestsAllowed)
	var waitGroup sync.WaitGroup

	for workerIndex := 1; workerIndex <= totalWorkers; workerIndex++ {
		waitGroup.Add(1)
		go worker(workerIndex, tasksChannel, &waitGroup)
	}

	for taskIndex := 1; taskIndex <= totalRequestsAllowed; taskIndex++ {
		tasksChannel <- taskIndex
	}

	close(tasksChannel)
	waitGroup.Wait()

	fmt.Println("All tasks processed.")
}

πŸ” How It Works

  1. βš™οΈ Goroutines: The main program creates a pool of workers (goroutines), each of which processes tasks from the channel.
  2. πŸ“¦ Task Distribution: Tasks are distributed across the workers through the channel, processed in parallel.
  3. πŸ› οΈ Synchronization: sync.WaitGroup ensures the program waits for all workers to finish before exiting.

πŸ”„ Workflow Summary

  1. πŸ“Œ Initialize Task Queue: Create a buffered channel to hold tasks.
  2. πŸš€ Create Workers: Launch totalWorkers worker goroutines.
  3. πŸ“€ Send Tasks: Send totalRequestsAllowed tasks to the channel.
  4. πŸ”’ Close the Channel: Indicate that no more tasks will be added.
  5. ⚑ Workers Process Tasks: Workers read from the channel and process tasks concurrently.
  6. ⏳ Wait for Completion: The program waits for all workers to finish.
  7. βœ… Final Message: A confirmation message is printed after all tasks are processed.

πŸ“Œ Business and Impact Value

  • 🏒 Business Efficiency: Helps optimize task processing, reducing bottlenecks in concurrent workflows.
  • πŸ’‘ Scalability: Can be extended to handle a higher number of tasks with minimal modifications.
  • ⏳ Time Optimization: Reduces processing time by leveraging concurrent workers.
  • πŸš€ Practical Use Cases: Ideal for job queues, background processing, and distributed task execution.

πŸ“Œ Usage

πŸ”§ Prerequisites

Ensure you have Go installed on your machine. You can verify this with:

$ go version

▢️ Running the Program

To execute the program, run:

$ go run main.go

About

This Go program efficiently processes tasks using goroutines, channels, and sync.WaitGroup, enabling concurrent execution and synchronization. Ideal for job queues and scalable task processing. πŸš€

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages