Aluode/PerceptionLabPortable
0
1import time2 3import pyqtgraph as pg4import pyqtgraph.multiprocess as mp5 6print( "\n=================\nParallelize")7 8## Do a simple task: 9## for x in range(N):10## sum([x*i for i in range(M)])11##12## We'll do this three times13## - once without Parallelize14## - once with Parallelize, but forced to use a single worker15## - once with Parallelize automatically determining how many workers to use16##17 18tasks = range(10)19results = [None] * len(tasks)20results2 = results[:]21results3 = results[:]22size = 200000023 24pg.mkQApp()25 26### Purely serial processing27start = time.time()28with pg.ProgressDialog('processing serially..', maximum=len(tasks)) as dlg:29 for i, x in enumerate(tasks):30 tot = 031 for j in range(size):32 tot += j * x33 results[i] = tot34 dlg += 135 if dlg.wasCanceled():36 raise Exception('processing canceled')37print( "Serial time: %0.2f" % (time.time() - start))38 39### Use parallelize, but force a single worker40### (this simulates the behavior seen on windows, which lacks os.fork)41start = time.time()42with mp.Parallelize(enumerate(tasks), results=results2, workers=1, progressDialog='processing serially (using Parallelizer)..') as tasker:43 for i, x in tasker:44 tot = 045 for j in range(size):46 tot += j * x47 tasker.results[i] = tot48print( "\nParallel time, 1 worker: %0.2f" % (time.time() - start))49print( "Results match serial: %s" % str(results2 == results))50 51### Use parallelize with multiple workers52start = time.time()53with mp.Parallelize(enumerate(tasks), results=results3, progressDialog='processing in parallel..') as tasker:54 for i, x in tasker:55 tot = 056 for j in range(size):57 tot += j * x58 tasker.results[i] = tot59print( "\nParallel time, %d workers: %0.2f" % (mp.Parallelize.suggestedWorkerCount(), time.time() - start))60print( "Results match serial: %s" % str(results3 == results))61 