Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

python - Concurrently run two functions that take parameters and return lists?

I understand that two functions can run in parallel using multiprocessing or threading modules, e.g. Make 2 functions run at the same time and Python multiprocessing for parallel processes.

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?

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:

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:

[out]:

[1, 4, 9, 16, 25] [1, 8, 27, 64, 125]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. But here's a couple things you can do:

1) Communicate back to spawning thread using 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:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...