Code-Memo

Concurrency and Parallelism

Concurrency refers to the ability of a system to handle multiple tasks at once, making progress on more than one task in overlapping time periods. It is often used to design systems that can handle multiple inputs and outputs (I/O) efficiently, such as web servers handling multiple requests concurrently.

Parallelism, on the other hand, involves executing multiple tasks simultaneously, using multiple processors or cores. It focuses on improving computational speed by dividing tasks across different processing units.

Threading vs. Multiprocessing

Threading

import threading

def worker():
    print("Thread working...")

# Create a thread
thread = threading.Thread(target=worker)
# Start the thread
thread.start()
# Wait for the thread to complete
thread.join()

Multiprocessing

import multiprocessing

def worker():
    print("Process working...")

# Create a process
process = multiprocessing.Process(target=worker)
# Start the process
process.start()
# Wait for the process to complete
process.join()