[PATCH 08 of 11] worker: partition a list (of tasks) into equal-sized chunks

Bryan O'Sullivan bos at serpentine.com
Sat Feb 9 08:06:48 CST 2013


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1360418465 0
# Node ID 023956ed1b098df7f93a5c1857b2f5ec00f3e45d
# Parent  d167155227b6752f01da2c0693f27cc42a7a8dde
worker: partition a list (of tasks) into equal-sized chunks

diff --git a/mercurial/worker.py b/mercurial/worker.py
--- a/mercurial/worker.py
+++ b/mercurial/worker.py
@@ -28,6 +28,19 @@ def countcpus():
 
     return 1
 
+def partition(lst, nslices):
+    '''partition a list into N slices of equal size'''
+    n = len(lst)
+    chunk, slop = n / nslices, n % nslices
+    end = 0
+    for i in xrange(nslices):
+        start = end
+        end = start + chunk
+        if slop:
+            end += 1
+            slop -= 1
+        yield lst[start:end]
+
 _numworkers = min(max(countcpus(), 4), 32)
 
 if os.name == 'nt':


More information about the Mercurial-devel mailing list