Running parallel task techniques in Python _python

Source: Internet
Author: User

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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.