简体   繁体   English

同时运行两个带有参数并返回列表的函数?

[英]Concurrently run two functions that take parameters and return lists?

I understand that two functions can run in parallel using multiprocessing or threading modules, eg Make 2 functions run at the same time and Python multiprocessing for parallel processes . 我知道两个函数可以使用multiprocessingthreading模块并行运行,例如, 使2个函数同时运行,Python对并行进程进行多处理

But the above examples only use print function. 但以上示例仅使用打印功能。 Is it possible to run functions that return a list in parallel in python, if so, how? 是否可以运行在python中并行返回列表的函数,如果可以,怎么办?

I've tried with threading: 我试过线程:

from threading import Thread
def func1(x):
    return [i*i for i in x]

def func2(x):
    return [i*i*i for i in x]

nums = [1,2,3,4,5]

p1 = Thread(target = func1(nums)).start()
p2 = Thread(target = func2(nums)).start()

print p1
print p2

but i got the follow error: 但我得到以下错误:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 761, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: 'list' object is not callable

None
None
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 761, in run
    self.__target(*self.__args, **self.__kwargs)
TypeError: 'list' object is not callable

I've tried inputing args parameter as a tuple, instead of a variable: 我尝试输入args参数作为元组,而不是变量:

import threading
from threading import Thread

def func1(x):
    return [i*i for i in x]

def func2(x):
    return [i*i*i for i in x]

nums = [1,2,3,4,5]

p1 = Thread(target = func1, args=(nums,)).start()
p2 = Thread(target = func2, args=(nums,)).start()

print p1, p2

but it only returns None None , the desired output should be: 但它仅返回None None ,所需的输出应为:

[out]: [OUT]:

[1, 4, 9, 16, 25] [1, 8, 27, 64, 125]

Thread's target function cannot return a value. 线程的目标函数无法返回值。 Or, I should say, the return value is ignored and as such, not communicated back to spawning thread. 或者,我应该说, return值将被忽略,因此不会传递回生成线程。 But here's a couple things you can do: 但是您可以执行以下几项操作:

1) Communicate back to spawning thread using Queue.Queue . 1)使用Queue.Queue通讯回到生成线程。 Note the wrapper around the original functions: 请注意原始功能的包装:

from threading import Thread
from Queue import Queue

def func1(x):
    return [i*i for i in x]

def func2(x):
    return [i*i*i for i in x]

nums = [1,2,3,4,5]

def wrapper(func, arg, queue):
    queue.put(func(arg))

q1, q2 = Queue(), Queue()
Thread(target=wrapper, args=(func1, nums, q1)).start() 
Thread(target=wrapper, args=(func2, nums, q2)).start() 

print q1.get(), q2.get()

2) Use global to access result lists in your threads, as well as the spawning process: 2)使用global访问线程中的结果列表以及生成过程:

from threading import Thread

list1=list()
list2=list()

def func1(x):
    global list1
    list1 = [i*i for i in x]

def func2(x):
    global list2
    list2 = [i*i*i for i in x]

nums = [1,2,3,4,5]

Thread(target = func1, args=(nums,)).start()
Thread(target = func2, args=(nums,)).start()

print list1, list2

Target should only receive the function name. 目标应仅接收功能名称。 The parameters should be passed with the parameter "args". 参数应与参数“ args”一起传递。 I cannot paste the code because I am answering from my mobile phone... ;-) 我无法粘贴代码,因为我正在用手机接听... ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM