python:programmes:multiprocessing1
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édente | ||
| python:programmes:multiprocessing1 [2012/11/11 17:54] – root | python:programmes:multiprocessing1 [2013/03/12 23:07] (Version actuelle) – root | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | Pourquoi il faut utiliser le multiprocessing et non le threading sous python : http:// | ||
| + | |||
| ====== Module multiprocessing ====== | ====== Module multiprocessing ====== | ||
| - | < | + | Méthode avec Pool : |
| + | |||
| + | < | ||
| # | # | ||
| Ligne 26: | Ligne 30: | ||
| | | ||
| print " | print " | ||
| + | </ | ||
| + | |||
| + | Méthode avec Queue (échanges interprocess) : | ||
| + | Extrait de http:// | ||
| + | <code python multiprocessing2.py> | ||
| + | # | ||
| + | |||
| + | from multiprocessing import Queue, Process, current_process | ||
| + | |||
| + | def worker(tasks, | ||
| + | # Get a tasks | ||
| + | t = tasks.get() | ||
| + | |||
| + | # Do operation | ||
| + | result = t * 2 | ||
| + | |||
| + | # Put the result in results queue | ||
| + | results.put([current_process().name, | ||
| + | |||
| + | if __name__ == ' | ||
| + | # Number of processes | ||
| + | n = 100 | ||
| + | |||
| + | # Create my tasks and results Queue | ||
| + | myTasks = Queue() | ||
| + | myResults = Queue() | ||
| + | |||
| + | # Create n process | ||
| + | Workers = [ Process(target=worker, | ||
| + | |||
| + | # Start processes | ||
| + | for each in Workers: | ||
| + | each.start() | ||
| + | |||
| + | # Create tasks | ||
| + | for each in range(n): | ||
| + | myTasks.put(each) | ||
| + | |||
| + | # Get the results | ||
| + | for each in range(n): | ||
| + | result = myResults.get() | ||
| + | print(" | ||
| </ | </ | ||
| Ligne 32: | Ligne 78: | ||
| Exemple de bench IO avec des threads : | Exemple de bench IO avec des threads : | ||
| - | < | + | < |
| # | # | ||
| # | # | ||
python/programmes/multiprocessing1.1352656492.txt.gz · Dernière modification : de root
