服务器之家

服务器之家 > 正文

Python中运行并行任务技巧

时间:2019-11-15 22:48     来源/作者:junjie

示例

标准线程多进程,生产者/消费者示例:
Worker越多,问题越大

复制代码代码如下:


# -*- 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

Map能够处理集合按顺序遍历,最终将调用产生的结果保存在一个简单的集合当中。

Python中运行并行任务技巧

 

复制代码代码如下:


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

 

标签:

相关文章

热门资讯

2022年最旺的微信头像大全 微信头像2022年最新版图片
2022年最旺的微信头像大全 微信头像2022年最新版图片 2022-01-10
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整
蜘蛛侠3英雄无归3正片免费播放 蜘蛛侠3在线观看免费高清完整 2021-08-24
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国
暖暖日本高清免费中文 暖暖在线观看免费完整版韩国 2021-05-08
返回顶部