Multiprocessing with python


Multiprocessing becomes handy in applications where time series data is to be analysed online. Python fortunately endows us with multiprocessing toolbox which is a relatiely easy tool for deploying non interacting processes to multiple cores.

Process

A process is a program in execution.

IO Bound process

A process which depends on I/O (Inputs/Outputs) from external hardware or other programs. "Downlading from the internet" is an IO Bound process. A python program inolving writting down video data on a hard disk drive is an IO Bound process.

Useful tips for writting a sequence of matrices as a video in python

OpenCV is a library used for handling ideo signals in python. VideoWriter object in OpenCV library facilitates writting the videos in a frame by frame fashion. (To create a video writer object the following code is used: video=cv2.VideoWriter(filename,fourcc,fps,(width,height))). By frame, I mean a grayscale image or a matrix having its entries between 0 and 255. "fourcc" specifies the compression standard used for writing the video. "width" and "height" corresponds to the number of columns and rows of frame (image frame is a matrix in 'uint8' format). The matrices can be sequentially written using "write" function in VideoWriter class (video.write(matrix), matrix should be a numpy array in 'uint8' format). Please make sure that you convert your matrix to BRG format before writing it in the video object. One can use: matrix=c2.cvtColor(matrix,cv2.COLOR_GRAY2BGR) for this purpose. Remember that the matrix is not a 2D array in 'uint8' format, instead its a multidimensional array in 'uint8' format. It's always good to include a video writer function that takes a sequence of matrices as an argument and returns a grayscale video sequence.

CPU Bound process

A process which inolves a lot of numerical computations. For example, "a python program to compute SVD" in execution is a CPU Bound process

Some interesting aspects about SVD

SVD is the process of splitting an arbitrary matrix \(A \in \mathbb{R}^{height \times width}\) as a product of three matrices. $$ A=U \Sigma V^T \text{ where } V \in \mathbb{R}^{width \times width}, U \in \mathbb{R}^{height \times height}, \Sigma \in \mathbb{R}^{height \times width} \text{ and } U^TU=UU^T=I_{height} \text{ and } VV^T=I_{width} \text { and } \Sigma \text{ is a diagonal matrix.} $$ The diagonal entries of \( \Sigma\) are known as the singular values.