python_multiprocessing
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteDernière révisionLes deux révisions suivantes | ||
python_multiprocessing [2021/08/19 06:05] – [Multiprocess dans kivy] serge | python_multiprocessing [2022/06/21 13:41] – [Mon exemple à moi archi simple] serge | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== Python: Multiprocessing et GIL ====== | ====== Python: Multiprocessing et GIL ====== | ||
- | |||
- | |||
=====Python est lent et n'est pas multiprocessus===== | =====Python est lent et n'est pas multiprocessus===== | ||
- | |||
Balivernes ... billevesées ... \\ | Balivernes ... billevesées ... \\ | ||
Toutes les Intelligences Artificielles ont une façade en python ... Dja dja (ah bon!)\\ | Toutes les Intelligences Artificielles ont une façade en python ... Dja dja (ah bon!)\\ | ||
L' | L' | ||
- | =====Ressources sur le module multiprocessing===== | ||
+ | =====GIL===== | ||
+ | * **https:// | ||
+ | Le module threading permet de lancer des tâches en parallèles, | ||
+ | Le GIL (Global Interpreteur Lock) évite de surcharger un thread. | ||
+ | |||
+ | Le module multiprocessing utilise réellement un cœur pour chaque tâche, et il est possible que chaque tâche communique avec une autre.\\ | ||
+ | Par contre, un processus n' | ||
+ | |||
+ | =====Ressources sur le module multiprocessing===== | ||
* http:// | * http:// | ||
* https:// | * https:// | ||
Ligne 16: | Ligne 21: | ||
* https:// | * https:// | ||
- | Mais comme souvent, les tutos sont hyper-compliqués, | + | Mais comme souvent, les tutos sont hyper-compliqués, |
===Remarques=== | ===Remarques=== | ||
Certains modules, tel que numpy, sont déjà multiprocess. | Certains modules, tel que numpy, sont déjà multiprocess. | ||
- | =====GIL===== | + | =====Exemples |
- | Le module threading permet | + | |
- | + | ||
- | Le GIL (Global Interpreteur Lock) évite de surcharger un thread. | + | |
- | + | ||
- | Le module multiprocessing utilise réellement un cœur pour chaque tâche, et il est possible que chaque tâche communique avec une autre.\\ | + | |
- | Par contre, un processus n' | + | |
- | + | ||
- | =====Exemples===== | + | |
Je vous propose des exemples simples: | Je vous propose des exemples simples: | ||
<file python multiprocessing_example_1.py> | <file python multiprocessing_example_1.py> | ||
""" | """ | ||
- | Example | + | Exemple |
documentation | documentation | ||
Ligne 75: | Ligne 72: | ||
""" | """ | ||
- | Exemple de 2 processus qui tournent en parallèle et échange | + | Exemple de 2 processus qui tournent en parallèle et échangent |
- | avec Pipe. | + | |
https:// | https:// | ||
Ligne 135: | Ligne 131: | ||
=====Multiprocess dans kivy===== | =====Multiprocess dans kivy===== | ||
Une application kivy qui excécute pyrealsense et coral dans un Process avec Pipe marche impeccable. | Une application kivy qui excécute pyrealsense et coral dans un Process avec Pipe marche impeccable. | ||
- | | + | |
* **[[https:// | * **[[https:// | ||
* Inferface graphique réalisée avec Kivy\\ | * Inferface graphique réalisée avec Kivy\\ | ||
* Suivi des personnes devant la caméra | * Suivi des personnes devant la caméra | ||
+ | =====multiprocessing.shared_memory ===== | ||
+ | ====Ressources==== | ||
+ | * **[[https:// | ||
+ | |||
+ | ====Mon exemple à moi archi simple==== | ||
+ | Je n'ai pas besoin de montrer que je suis très fort pour me faire embaucher chez GAFAM. | ||
+ | |||
+ | <code python> | ||
+ | from time import time, sleep | ||
+ | import random | ||
+ | from multiprocessing import Process | ||
+ | from multiprocessing.sharedctypes import Value | ||
+ | |||
+ | class SharedMemory: | ||
+ | def __init__(self): | ||
+ | self.val = Value(" | ||
+ | print(self.val, | ||
+ | my_proc = Process(target=another_process, | ||
+ | my_proc.start() | ||
+ | |||
+ | def shared_memory_master(self): | ||
+ | t = time() | ||
+ | while time() - t < 4: | ||
+ | print(f" | ||
+ | sleep(0.1) | ||
+ | |||
+ | def another_process(val): | ||
+ | t = time() | ||
+ | while time() - t < 3: | ||
+ | n = random.randint(-1000, | ||
+ | print(f" | ||
+ | val.value = n | ||
+ | sleep(0.3) | ||
+ | |||
+ | if __name__ == " | ||
+ | sh = SharedMemory() | ||
+ | sh.shared_memory_master() | ||
+ | </ | ||
+ | ====Exemple d' | ||
+ | |||
+ | <code python> | ||
+ | from multiprocessing import shared_memory | ||
+ | |||
+ | shm_a = shared_memory.SharedMemory(create=True, | ||
+ | m = shm_a.buf | ||
+ | print(' | ||
+ | |||
+ | buffer = shm_a.buf | ||
+ | print(' | ||
+ | |||
+ | # Modify multiple at once | ||
+ | buffer[:4] = bytearray([22, | ||
+ | # Modify single byte at a time | ||
+ | buffer[4] = 100 | ||
+ | |||
+ | # Attach to an existing shared memory block | ||
+ | shm_b = shared_memory.SharedMemory(shm_a.name) | ||
+ | |||
+ | import array | ||
+ | |||
+ | # Copy the data into a new array.array | ||
+ | ar = array.array(' | ||
+ | print(' | ||
+ | |||
+ | # Modify via shm_b using bytes | ||
+ | shm_b.buf[: | ||
+ | |||
+ | # Access via shm_a | ||
+ | print(' | ||
+ | |||
+ | # Close each SharedMemory instance | ||
+ | shm_b.close() | ||
+ | shm_a.close() | ||
+ | |||
+ | # Call unlink only once to release the shared memory | ||
+ | shm_a.unlink() | ||
+ | </ | ||
+ | |||
+ | ====Exemple de partage de numpy array==== | ||
+ | <code python> | ||
+ | from multiprocessing import Process | ||
+ | from multiprocessing.managers import SharedMemoryManager | ||
+ | from multiprocessing.shared_memory import SharedMemory | ||
+ | |||
+ | import numpy as np | ||
+ | |||
+ | |||
+ | def test(shared_mem: | ||
+ | a = np.frombuffer(shared_mem.buf, | ||
+ | a[0] = -a[0] | ||
+ | |||
+ | |||
+ | if __name__ == " | ||
+ | # Create the array | ||
+ | N = int(10) | ||
+ | unshared_arr = np.random.rand(N) | ||
+ | DTYPE = unshared_arr.dtype | ||
+ | with SharedMemoryManager() as smm: | ||
+ | shared_mem = smm.SharedMemory(size=unshared_arr.nbytes) | ||
+ | arr = np.frombuffer(shared_mem.buf, | ||
+ | arr[:] = unshared_arr | ||
+ | print(" | ||
+ | |||
+ | # Create, start, and finish the child processes | ||
+ | p = Process(target=test, | ||
+ | p.start() | ||
+ | p.join() | ||
+ | # Printing out the changed values | ||
+ | print(" | ||
+ | </ | ||
{{tag> kivy python sb }} | {{tag> kivy python sb }} |
python_multiprocessing.txt · Dernière modification : 2022/06/21 13:42 de serge