Outils pour utilisateurs

Outils du site


python_multiprocessing

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentesRévision précédente
Prochaine révision
Révision précédente
python_multiprocessing [2022/06/15 11:55] – [Multiprocess dans kivy] sergepython_multiprocessing [2022/06/21 13:42] (Version actuelle) – [Mon exemple à moi archi simple] serge
Ligne 140: Ligne 140:
   * **[[https://docs.python.org/fr/3.9/library/multiprocessing.shared_memory.html|docs.python.org/fr Mémoire partagée en accès direct depuis plusieurs processus¶]]**    * **[[https://docs.python.org/fr/3.9/library/multiprocessing.shared_memory.html|docs.python.org/fr Mémoire partagée en accès direct depuis plusieurs processus¶]]** 
  
 +====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("i", -4000)
 +        print(self.val, self.val.value)
 +        my_proc = Process(target=another_process,  args=(self.val, ))
 +        my_proc.start()
 +
 +    def shared_memory_master(self):
 +        t = time()
 +        while time() - t < 4:
 +            print(f"Lecture de {self.val}: {self.val.value}")
 +            sleep(0.1)
 +
 +def another_process(val):
 +    t = time()
 +    while time() - t < 3:
 +        n = random.randint(-1000, 1000)
 +        print(f"Maj de n = {n}")
 +        val.value = n
 +        sleep(0.3)
 +
 +if __name__ == "__main__":
 +    sh = SharedMemory()
 +    sh.shared_memory_master()
 +</code>
 ====Exemple d'utilisation bas niveau d'instances de SharedMemory==== ====Exemple d'utilisation bas niveau d'instances de SharedMemory====
  
Ligne 180: Ligne 214:
 </code> </code>
  
 +====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: SharedMemory, dtype):
 +    a = np.frombuffer(shared_mem.buf, dtype=dtype)
 +    a[0] = -a[0]
 +
 +
 +if __name__ == "__main__":
 +    # 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, dtype=DTYPE)
 +        arr[:] = unshared_arr
 +        print("Originally, the first two elements of arr = %s" % (arr[:2]))
 +
 +        # Create, start, and finish the child processes
 +        p = Process(target=test, args=(shared_mem, DTYPE))
 +        p.start()
 +        p.join()
 +
 +        # Printing out the changed values
 +        print("Now, the first two elements of arr = %s" % arr[:2])
 +</code>
  
  
 {{tag> kivy python sb }} {{tag> kivy python sb }}
python_multiprocessing.1655294124.txt.gz · Dernière modification : 2022/06/15 11:55 de serge