We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Parallel Python with Process Pools step 9 of 24
Split Work into Chunks for a Process Pool
A process pool sends work to its workers in messages. If you send 10,000 small jobs one at a time, the pool sends 10,000 messages, and the cost of sending them can be bigger than the work itself. The fix is to send the jobs in chunks, which are groups of jobs that the pool sends to a worker in one message. Write the function that splits a list into chunks.
chunks([1, 2, 3, 4, 5, 6, 7], 3)
# [[1, 2, 3], [4, 5, 6], [7]]
What to write
Fill in chunks(items, size):
- Return a list of lists. Each inner list holds the next
sizeitems, in their original order. - The last chunk holds whatever is left, so it can be shorter than
size. - An empty
itemsgives an empty list. - If
sizeis less than 1, raiseValueError.
What the driver checks
Keep solve as it is. It returns:
chunks: the list your function returned.messages: the number of chunks, which is how many messages a pool would send for this work.rejected:Trueif your function raisedValueError. Thenchunksis empty andmessagesis 0.
ProcessPoolExecutor.map takes a chunksize argument that does this for
you. The next article explains how to choose it.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.