top of page

TechBobbles

tech logo with triangle shape.jpg
Writer's pictureAdmin

Parallel Processing in PowerShell and Python

Parallel Processing

Parallel processing enable users to run commands/Scrips simultaneously, it helps to improve script efficiency. This can be achieved both in python and PowerShell. Below are the example of parallel processing python and PowerShell.


Parallel Processing in PowerShell with Start-Job

PowerShell’s Start-Job cmdlet allows you to run script blocks as background jobs. This is particularly useful for tasks that can be executed independently, such as processing multiple files or making multiple web requests.

Example:

$jobs = @()

1..10 | ForEach-Object {

    $jobs += Start-Job -ScriptBlock {

        Start-Sleep -Seconds (Get-Random -Minimum 1 -Maximum 5)

        "Job $_ completed"

    }

}

 

# Wait for all jobs to complete

$jobs | ForEach-Object { $_ | Wait-Job }

 

# Retrieve job results

$results = $jobs | ForEach-Object { Receive-Job -Job $_ }

$results

 

# Clean up jobs

$jobs | ForEach-Object { Remove-Job -Job $_ }


In this example, we start 10 background jobs that each sleep for a random number of seconds between 1 and 5, then output a completion message. We wait for all jobs to complete, retrieve their results, and finally clean up the jobs.




Parallel Processing in Python with concurrent.futures

Python’s concurrent.futures module provides a high-level interface for asynchronously executing callables. This module is ideal for parallel processing tasks that can be broken down into smaller, independent units of work.

Example:

import concurrent.futures

import time

import random

 

def task(n):

    time.sleep(random.randint(1, 5))

    return f"Task {n} completed"

 

with concurrent.futures.ThreadPoolExecutor() as executor:

    futures = [executor.submit(task, i) for i in range(10)]

    for future in concurrent.futures.as_completed(futures):

        print(future.result())


In this example, we use a ThreadPoolExecutor to run 10 tasks in parallel. Each task sleeps for a random number of seconds between 1 and 5, then returns a completion message.


Parallel processing can be beneficial while doing multiple changes at one point of time or when running Jobs are interdependent. It can make the administrator life lot easy.

Comments


Thanks for visiting 

Bangalore,

Siddartha1192@gmail.com

+919148014455

  • Linkedin
  • Wix Facebook page
  • Wix Twitter page

Subscribe to get exclusive updates

bottom of page