Example
Standard thread multi-process, producer/Consumer example:
The more the worker, the bigger the problem.
Copy Code code as follows:
#-*-Coding:utf8-*-
Import OS
Import time
Import Queue
Import threading
From PIL import Image
def create_thumbnail (filename, size= (128, 128)):
Try
FP, FMT = Filename.rsplit ('. ', 1)
im = Image.open (filename)
Im.thumbnail (size, Image.antialias)
Im.save (fp + ' _ ' + ' x ') join (str (i) for I in size) + '. ' +FMT), Im.format)
Return '%s thumbnail success! '% filename
Except Exception:
Return '%s thumbnail failed! '% filename
def get_image_paths (folder):
return [Os.path.join (folder, F) for F in Os.listdir (folder) if ' PNG ' in F]
Class Consumer (threading. Thread):
def __init__ (self, queue):
Threading. Thread.__init__ (self)
Self._queue = Queue
def run (self):
While True:
Content = Self._queue.get ()
If Isinstance (content, str) and content = = ' Quit ':
Break
Respone = create_thumbnail (content)
print ' Bye bye! '
Def Producer ():
filenames = get_image_paths (' images ')
Queue = Queue.queue ()
Worker_threads = Build_worker_pool (queue, 4)
Start_time = Time.time ()
For filename in filenames:
Queue.put (filename)
For worker in Worker_threads:
Queue.put (' Quit ')
For worker in Worker_threads:
Worker.join ()
Print Time.time ()-start_time
def build_worker_pool (queue, size):
Workers = []
For _ in range (size):
Worker = Consumer (queue)
Worker.start ()
Workers.append (worker)
Return workers
if __name__ = = ' __main__ ':
Producer ()
Map
The map can process the collection in sequential traversal and ultimately save the result of the call in a simple collection.
Copy Code code as follows:
#-*-Coding:utf8-*-
Import OS
Import time
From multiprocessing import Pool
From PIL import Image
def create_thumbnail (filename, size= (128, 128)):
Try
FP, FMT = Filename.rsplit ('. ', 1)
im = Image.open (filename)
Im.thumbnail (size, Image.antialias)
Im.save (fp + ' _ ' + ' x ') join (str (i) for I in size) + '. ' +FMT), Im.format)
Return '%s thumbnail success! '% filename
Except Exception:
Return '%s thumbnail failed! '% filename
def get_image_paths (folder):
return [Os.path.join (folder, F) for F in Os.listdir (folder) if ' PNG ' in F]
def main ():
filenames = get_image_paths (' images ')
Start_time = Time.time ()
Pool = Pool (4)
Pool.map (Create_thumbnail, filenames)
Pool.close ()
Pool.join ()
Print Time.time ()-start_time
if __name__ = = ' __main__ ':
Main ()