when dealing with fileswe have to think about the exact layout of the bytes in the compressed file our file will store two byte little-endian integers at the beginning of the file representing the width and height of the completed file then it will write bytes representing the bit chunks of each row now before we start designing concurrent system to build such compressed imageswe should ask fundamental questionis this application / -bound or cpu-boundmy answerhonestlyis " don' knowi' not sure whether the app will spend more time loading data from disk and writing it back or doing the compression in memory suspect that it is cpu bound app in principlebut once we start passing image strings into subprocesseswe may lose any benefit of parallelism the optimal solution to this problem is probably to write or cython extensionbut let' see how far we can get in pure python we'll build this application using bottom-up design that way we'll have some building blocks that we can combine into different concurrency patterns to see how they compare let' start with the code that compresses -bit chunk using run-length encodingfrom bitarray import bitarray def compress_chunk(chunk)compressed bytearray(count last chunk[ for bit in chunk[ :]if bit !lastcompressed append(count ( last)count last bit count + compressed append(count ( last)return compressed this code uses the bitarray class for manipulating individual zeros and ones it is distributed as third-party modulewhich you can install with the command pip install bitarray the chunk that is passed into compress_chunks is an instance of this class (although the example would work just as well with list of booleansthe primary benefit of the bitarray in this case is that when pickling them between processesthey take up an th of the space of list of booleans or bytestring of and thereforethey pickle faster they are also bit (pun intendedeasier to work with than doing ton of bitwise operations