{Python extendibility}

Python Catania - Stefano Borzì

   Python extendibility

Programming languages comparison

   Python extendibility

Programming languages comparison

print('Hello World!')
#include <iostream>
using namespace std;

int main() {
    cout << "Hello World!" << endl;
    return 0;
}
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}
打印 你好世界
   Python extendibility

Programming languages comparison

" The ratings are based on the number of skilled engineers world-wide, courses and third party vendors "

   Python extendibility

" In 2024, Python overtook JavaScript as the most popular language on GitHub "

   Python extendibility

CPython

   Python extendibility
counts = {}
for x in data:
    counts[x] = counts.get(x, 0) + 1
from collections import Counter

counts = Counter(data)
   Python extendibility
import time
from collections import Counter

data = [i % 10 for i in range(10_000_000)]

# Manual loop
t0 = time.time()
counts = {}
for x in data:
    counts[x] = counts.get(x, 0) + 1

t1 = time.time()
print("Manual loop:", t1 - t0)

# Counter
t0 = time.time()
counts2 = Counter(data)
t1 = time.time()
print("Counter:", t1 - t0)

Manual loop: 0.727339506149292


Counter:

0.31299853324890137

Implemented in

   Python extendibility
   Python extendibility

Python extendibility

#define PY_SSIZE_T_CLEAN
#include <Python.h>

// Define the C function that implements "add"
static PyObject* myext_add(PyObject* self, PyObject* args) {
    double a, b;
    if (!PyArg_ParseTuple(args, "dd", &a, &b)) {
        return NULL;  // invalid arguments
    }
    double result = a + b;
    return Py_BuildValue("d", result);  // return a Python float
}

// Method definitions
static PyMethodDef MyExtMethods[] = {
    {"add", myext_add, METH_VARARGS, "Add two numbers"},
    {NULL, NULL, 0, NULL}
};

// Module definition
static struct PyModuleDef myextmodule = {
    PyModuleDef_HEAD_INIT,
    "myext",   // module name
    NULL,      // optional docstring
    -1,        // size of per-interpreter state or -1
    MyExtMethods
};

// Module initialization
PyMODINIT_FUNC PyInit_myext(void) {
    return PyModule_Create(&myextmodule);
}

myext.c

   Python extendibility

Python extendibility

from setuptools import setup, Extension

module = Extension("myext", sources=["myext.c"])

setup(
    name="myext",
    version="1.0",
    description="Simple example of Python C extension",
    ext_modules=[module],
)

$ pip install .

setup.py

import myext

print(myext.add(3.5, 4.2))
   Python extendibility

End

{Python multithreading}

Python Catania - Stefano Borzì

   Python multithreading
from time import sleep

arr = range(100)

for x in arr:
  print(x)
  sleep(1)
   Python multithreading

tqdm

from time import sleep
from tqdm import tqdm

arr = range(100)

for x in tqdm(arr):
  # print(x)
  sleep(1)

$ pip install tqdm

   Python multithreading

pathos

from time import sleep
import pathos as pa

arr = range(100)

def parallel():
   sleep(1)

ncpu = pa.helpers.cpu_count()
with pa.multiprocessing.ProcessingPool(ncpu) as p:
    p.map(parallel, arr), total=len(arr)

$ pip install pathos

   Python multithreading

pathos + tqdm

from time import sleep
from tqdm import tqdm
import pathos as pa

arr = range(100)

def parallel():
   sleep(1)

ncpu = pa.helpers.cpu_count()
with pa.multiprocessing.ProcessingPool(ncpu) as p:
    list(tqdm(p.imap(parallel, arr), total=len(arr)))
   Python multithreading

multiprocessing vs multithreading

   Python multithreading

multithreading in Python 3.12 (single-thread)

   Python multithreading

multithreading - Python GIL

Python has a built-in mechanism that limits how threads are executed, and it’s called the Global Interpreter Lock, or GIL.

   Python multithreading

CPU simulation - single thread (gil)

   Python multithreading

CPU simulation - multithreading (no-gil)

   Python extendibility

exit(0)

Thank you!

Python Catania slides

By Stefano Borzì

Python Catania slides

  • 28