multitraitement vs threading

Préparez notebookimer pour mesurer le temps

parallel.py


#!/usr/bin/python

from multiprocessing import Process
from threading       import Thread

def thread(function, args, processes):

    process_list = []
    for i in xrange(processes):
        p = Thread(target=function, args=args)
        p.start()
        process_list.append(p)
    for i in process_list:
        i.join()

def multiprocess(function, args, processes):

    process_list = []
    for i in xrange(processes):
        p = Process(target=function, args=args)
        p.start()
        process_list.append(p)
    for i in process_list:
        i.join()

if __name__ == '__main__':

    def worker(x, y):
        for a in xrange(x):
            for b in xrange(y):
                c = a * b

    from laptimer import LapTimer

    timer = LapTimer("multiprocessing vs threading")

    # multiprocess
    multiprocess(worker, [100, 200], 10)
    timer.lap("multiprocess")

    # threading
    thread(worker, [100, 200], 10)
    timer.lap("threading")

    timer.get_lap()

résultat

-------------------------
multiprocessing vs threading
   Lap : Total : message
-------------------------
 0.037 : 0.037 : multiprocess
 0.017 : 0.055 : threading

Recommended Posts

multitraitement vs threading
mémorandum de multitraitement
Multitraitement instantané
Compréhension complète du threading Python et du multitraitement