Outils pour utilisateurs

Outils du site


python:programmes:multiprocessing1

Ceci est une ancienne révision du document !


Multithreading (module multiprocessing)

#!/usr/bin/env python

from multiprocessing import Pool, cpu_count
from time import sleep

def f(x,y):
    sleep(2)
    return x*y

def k(x,y):
    sleep(2)
    return x+y

if __name__ == '__main__':
    result=[]
    pool = Pool(processes = cpu_count())  # start x worker processes
    result.append(pool.apply_async(f, (5,10,)))  # evaluate "f(10)" asynchronously
    result.append(pool.apply_async(k, (10,10,)))  # evaluate "f(10)" asynchronously

    for r in result:
         try:
             print r.get(timeout=3)                     # timeout 3 secs
         except:
             print "timeout..."
python/programmes/multiprocessing1.1352655902.txt.gz · Dernière modification : 2012/11/11 17:45 de root