Python中的并行处理/线程 [英] Parallel processing / threading in Python

查看:62
本文介绍了Python中的并行处理/线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在应用程序中使用多处理或线程在后台执行一些耗时的操作.我看了很多例子,但是我仍然无法实现我想要的.我正在尝试加载一堆图像,每个图像都需要几秒钟.我希望先加载第一张图像,然后再将其他图像加载到后台,然后存储在列表中(以供以后使用),而程序仍在做其他事情(例如允许我的GUI上的控件继续工作).如果我有类似以下示例的内容,我该怎么做?我应该使用多处理还是线程处理?

I am looking to use multiprocessing or threading in my application to do some time-consuming operations in the background. I have looked at many examples, but I still have been unable to achieve what I want. I am trying to load a bunch of images, each of which takes several seconds. I would like the first image to be loaded and then have the others loading in the background and being stored in a list (to use later) while the program is still doing other things (like allowing controls on my GUI to still work). If I have something like the example below, how can I do this? And should I use multiprocessing or threading?

class myClass():
    def __init__(self, arg1, arg2):
        #initializes some parameters

    def aFunction(self):
        #does some things
        #creates multiple processes or threads that each call interestingFunc
        #continues doing things

    def interestingFunc(self):
        #performs operations

m = myClass()

推荐答案

您可以使用任何一种方法.让您的ProcessThread执行其工作,然后将结果放入Queue中.然后,您的主线程/进程可以在闲暇时将结果从队列中移出并对其进行处理.这是一个多处理示例.

You can use either approach. Have your Process or Thread perform its work and then put the results onto a Queue. Your main thread/process can then, at its leisure, take the results off the queue and do something with them. Here's an example with multiprocessing.

from multiprocessing import Process, Queue

def load_image(img_file, output_q):
    with open(img_file, 'rb') as f:
        img_data = f.read()
        # perform processing on img_data, then queue results
        output_q.put((img_file, img_data))

result_q = Queue()
images = ['/tmp/p1.png', '/tmp/p2.jpg', '/tmp/p3.gif', '/tmp/p4.jpg']

for img in images:
    Process(target=load_image, args=(img, result_q)).start()

for i in range(len(images)):
    img, data = result_q.get()
    # do something with the image data
    print "processing of image file %s complete" % img

这假定处理顺序对您的应用程序并不重要,即每个文件中的图像数据可能以任何特定顺序加载到队列中.

This assumes that the order of processing is not significant to your application, i.e. the image data from each file might be loaded onto the queue in any particular order.

这篇关于Python中的并行处理/线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆