Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| technique:python:bash_multithreading [2021/05/29 23:34] – francoisa | technique:python:bash_multithreading [2024/04/02 15:08] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| - | ====== Python, bash et Multithreading ====== | + | **Cette page est obsolète. Veuillez accéder |
| - | + | ||
| - | ===== Script bash ===== | + | |
| - | + | ||
| - | Lancer un script bash avec nice 19 (faible priorité): | + | |
| - | + | ||
| - | <code python> | + | |
| - | from subprocess import Popen | + | |
| - | + | ||
| - | resultat = Popen([" | + | |
| - | resultat.wait() | + | |
| - | + | ||
| - | if resultat.returncode == 0: | + | |
| - | print(" | + | |
| - | + | ||
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | ===== Tâche de fond ===== | + | |
| - | + | ||
| - | Cas de processus en tâche de fond et qui redonne la main au fil principal | + | |
| - | + | ||
| - | < | + | |
| - | from multiprocessing import Process | + | |
| - | + | ||
| - | def tache_en_fond(mes_vars): | + | |
| - | # Fait des trucs en tâche de fond | + | |
| - | + | ||
| - | if __name__ == ' | + | |
| - | p = Process(target=tache_en_fond, | + | |
| - | p.start() | + | |
| - | print(" | + | |
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | ===== Multithreading ===== | + | |
| - | + | ||
| - | Le multithreading impose de décomposer plusieurs tâches dans plusieurs threads | + | |
| - | + | ||
| - | [[https://pythonguides.com/python-threading-and-multithreading/|https:// | + | |
| - | + | ||
| - | [[https:// | + | |
| - | + | ||
| - | [[https:// | + | |
| - | + | ||
| - | ===== Gestion des processus et du loadaverage ===== | + | |
| - | + | ||
| - | ==== Nombre de coeurs d'une machine ==== | + | |
| - | + | ||
| - | from psutil import cpu_count | + | |
| - | + | ||
| - | # ou from os import cpu_count | + | |
| - | + | ||
| - | cpu_count() | + | |
| - | + | ||
| - | ==== Récupérer le loadaverage ==== | + | |
| - | + | ||
| - | Le loadaverage se récupère facilement avec : | + | |
| - | < | + | |
| - | + | ||
| - | open("/ | + | |
| - | + | ||
| - | </ | + | |
| - | + | ||
| - | Et un petit script qui permet de lancer un truc quand la charge redescend. | + | |
| - | + | ||
| - | < | + | |
| - | import time | + | |
| - | + | ||
| - | #wait if load average is too high | + | |
| - | def load_average_wait(treshold=1): | + | |
| - | # If server is above the treshold value, wait for lower load before continue | + | |
| - | while float(open("/ | + | |
| - | time.sleep(10) | + | |
| - | + | ||
| - | # my script part that eat a lot... | + | |
| - | while True: | + | |
| - | # Encode video, is there anything else eating so much cpu in the universe ? | + | |
| - | encode_video() | + | |
| - | # wait if cpu start boiling... | + | |
| - | load_average_wait(1.5) | + | |
| - | + | ||
| - | </ | + | |
| - | + | ||