Python

Write an awesome doc for Python. A very nice an practical one extracted from Python official documentation.

View on GitHub

Multiprocessing

[!TIP]

We also have Semaphore which enables your processes to access same resource simultaneously, and how many processes can access the resource is configurable:

import random
from multiprocessing import Process, Semaphore
from time import sleep
from typing import Any


def worker(semaphore: Any, index: int) -> None:
    with semaphore:
        print(f"Process {index}# accessed some resource.")
        sleep(random.uniform(1, 3))
    print(f"\tProcess {index}# has been completed")


if __name__ == "__main__":
    semaphore = Semaphore(2)

    processes = [Process(target=worker, args=(semaphore, index)) for index in range(6)]

    [process.start() for process in processes]
    [process.join() for process in processes]

Queue