Python:如何对返回多个值的函数进行多线程处理 [英] Python: How to Multi-Thread a Function that Returns Multiple Values

查看:495
本文介绍了Python:如何对返回多个值的函数进行多线程处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,该程序具有两个函数,每个函数返回2个值.一切运行正常,没有任何并行化.但是,我希望实现的是在每个函数各自的线程/进程中运行它们.程序在并行化之前是这样的:

I have a program that has two functions that returns 2 values for each function. Everything runs fine without any parallelization. However what I am hoping to achieve is to run each function in its own thread/process. This what the program looks like before parallelization:

 def func1():
    x = 2
    y =5
    return x,y

  def func2():
    a = 4
    b = 3 
    return a,b

  func1ResultX , func1ResultY = func1()
  func2ResultA , funct2ResultB = func2()

我知道我可以使用threadthreadingmultiprocessing来实现这一目标.但是尚不清楚如何构造程序以返回结果或返回多个值.我的初步努力产生了以下结果

I understand that I can use thread , threading, or multiprocessing to achieve this. But it is unclear how to structure the program to return the result, or return the multiple values. My initial efforts yielded the following results

使用thread

function1ResultX, function1ResultY = thread.start_new_thread(func1)
function2ResultA, function2ResultB = thread.start_new_thread(func2)

结果: TypeError: 'int' object is not iterable

使用multiprocessing

function1ResultX , function1Resulty = multiprocessing.Process(target=func1)
function2ResultA, function2ResultB = thread.start_new_thread(func2)

结果: TypeError: 'Process' object is not iterable

使用threading我看到示例类似于以下内容:

Using threading I see the example is something akin to the following:

t1 = threading.Thread(target=someFunc)
t1.start()
t1.join()

但是我不知道如何修改示例以适合返回两个值的函数,也不清楚如何仅返回一个值.

But I don't know how to modify the example to fit my function which returns two values, nor is it clear to me how to return even only one value.

我尝试将函数减少为仅返回一个值,如下所示:

I tried reducing my function to having only returning only one value like so:

 def func1():
   MyNewList = [2,5]
   return MyNewList

并结合使用threading模块的方法:

And incorporating the method for using the threading module:

    MyNewList = threading.Thread(target=func1)
    MyNewList.start()
    MyNewList.join()

    print MyNewList[0]
    print MyNewList[1]

虽然我希望输出是25,但是却出现了错误:

While I expect the output to be 2 and 5, I got instead the error:

TypeError: 'Thread' object does not support indexing

简而言之,我的问题是:哪种并行化方法/模块最适合我的情况,我应该如何构造我的代码以适应它?也欢迎就我的概念性误解提供任何反馈.预先谢谢你.

In a nutshell my question is: which is the parallelization method/module best suited for my case, and how should I structure my code to accommodate it? Any feedback on my conceptual misunderstanding is also welcome. Thank you in advance.

推荐答案

您可能需要稍微修改一下函数才能使用

You might want to modify your function a little bit to use Queue for getting results.

import threading, Queue

def func1(queue):
    x = 2
    y = 5
    queue.put((x, y))

queue = Queue.Queue()
new_thread = threading.Thread(target=func1, args=(queue, ))
new_thread.start()
new_thread.join()

x, y = queue.get()

这篇关于Python:如何对返回多个值的函数进行多线程处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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