nanyang-system-developers

Concurrent Programming

Overview

Concurrent programming is a way to make your computer do multiple things at the same time. Imagine you are working on a school project and you need to write a report, do some research online, and chat with your friends for help. Instead of doing these tasks one after another, you can do them all at once. This is similar to what concurrent programming does for your computer programs.

When to Use Concurrent Programming

Concurrent programming is useful when you have tasks that can run independently and simultaneously. For example:

Multiprocessing, Threading, and Asyncio in Python

Python provides several ways to achieve concurrency:

Multiprocessing

Multiprocessing involves running multiple processes, each with its own memory space. This is like having multiple computers working on different tasks. It is useful for CPU-bound tasks, which require a lot of computation.

Threading

Threading involves running multiple threads within the same process, sharing the same memory space. This is like having multiple people working on different parts of the same project. It is useful for I/O-bound tasks, such as reading and writing files or network operations.

Asyncio

Asyncio is a library that allows you to write asynchronous code using async and await keywords. It is useful for tasks that spend a lot of time waiting, such as web scraping or handling multiple user requests in a web server.

Common Python Libraries

Here are some common Python libraries that use these concurrency techniques:

By using these libraries, you can make your programs more efficient and responsive, especially when working on complex school projects that require handling multiple tasks at once.

Concurrent Programming vs. Imperative Programming

Concurrent programming allows multiple tasks to run at the same time, while imperative programming executes tasks one after another in a sequence. Here’s a simple example to illustrate the difference:

Imperative Programming Example

def task1():
    print("Task 1: Writing report")
    # Simulate time taken to write report
    time.sleep(2)

def task2():
    print("Task 2: Doing research")
    # Simulate time taken to do research
    time.sleep(2)

def task3():
    print("Task 3: Chatting with friends")
    # Simulate time taken to chat with friends
    time.sleep(2)

task1()
task2()
task3()

Concurrent Programming Example

import asyncio

async def task1():
    print("Task 1: Writing report")
    await asyncio.sleep(2)

async def task2():
    print("Task 2: Doing research")
    await asyncio.sleep(2)

async def task3():
    print("Task 3: Chatting with friends")
    await asyncio.sleep(2)

async def main():
    await asyncio.gather(task1(), task2(), task3())

asyncio.run(main())

In the imperative example, each task waits for the previous one to finish. In the concurrent example, all tasks run simultaneously, making the program more efficient.

By understanding and using concurrent programming, you can make your school projects more efficient and handle multiple tasks at the same time, just like you do in real life!