Posts

Showing posts from 2024

Learning to use multiprocessing in Python using Pebble package

Image
A popular thread and concurrency management package in Python that I generally rely on is the pebble package. Pebble simplifies parallel task execution in Python, making it easier to leverage the full computing power of multicore systems and improve the performance of concurrent applications. I intend to give a small demo of how to implement this package in a regular piece of code. Let's consider this piece of code that iterates over an array of tuples. Each tuple holds our task name and time duration. We are passing each tuple as argument to a function that performs a basic task(e.g. sleep). There is no concurrency involved here as each loop waits until the previous loop completes all tasks. import time work = ([ "A" , 5 ], [ "B" , 2 ], [ "C" , 1 ], [ "D" , 3 ]) def work_log (work_data): print ( " Process %s waiting %s seconds" % (work_data[ 0 ], work_data[ 1 ])) time.sleep( int (work_data[ 1 ])) print ( " Process