# HG changeset patch # User Ludovic Chabant # Date 1435528378 25200 # Node ID 171dde4f61dcb64f43309e36f2df0f95c5715b34 # Parent dc8518c51cbed45d98539e931a7c7669b995becf performance: Add profiling to the asset pipeline workers. diff -r dc8518c51cbe -r 171dde4f61dc piecrust/processing/pipeline.py --- a/piecrust/processing/pipeline.py Sun Jun 28 14:52:37 2015 -0700 +++ b/piecrust/processing/pipeline.py Sun Jun 28 14:52:58 2015 -0700 @@ -255,12 +255,19 @@ ctx.pool.queue.put_nowait(job) def _createWorkerPool(self): + import sys + + main_module = sys.modules['__main__'] + is_profiling = os.path.basename(main_module.__file__) in [ + 'profile.py', 'cProfile.py'] + pool = _WorkerPool() for i in range(self.num_workers): ctx = ProcessingWorkerContext( self.app.root_dir, self.out_dir, self.tmp_dir, pool.queue, pool.results, pool.abort_event, self.force, self.app.debug) + ctx.is_profiling = is_profiling ctx.enabled_processors = self.enabled_processors ctx.additional_processors = self.additional_processors w = multiprocessing.Process( diff -r dc8518c51cbe -r 171dde4f61dc piecrust/processing/worker.py --- a/piecrust/processing/worker.py Sun Jun 28 14:52:37 2015 -0700 +++ b/piecrust/processing/worker.py Sun Jun 28 14:52:58 2015 -0700 @@ -23,6 +23,21 @@ def worker_func(wid, ctx): + if ctx.is_profiling: + try: + import cProfile as profile + except ImportError: + import profile + + ctx.is_profiling = False + profile.runctx('_real_worker_func(wid, ctx)', + globals(), locals(), + filename='PipelineWorker-%d.prof' % wid) + else: + _real_worker_func(wid, ctx) + + +def _real_worker_func(wid, ctx): logger.debug("Worker %d booting up..." % wid) w = ProcessingWorker(wid, ctx) w.run() @@ -40,6 +55,7 @@ self.abort_event = abort_event self.force = force self.debug = debug + self.is_profiling = False self.enabled_processors = None self.additional_processors = None