什么是Python中的最佳方式来调用单独的线程同样的功能?
问题描述:
在单独的线程中调用相同的函数函数的最佳方式是什么,并且每个实例都有一个带有返回值的单独列表,而不重复函数?什么是Python中的最佳方式来调用单独的线程同样的功能?
例子:
import threading
def function(a):
returned_values = []
ct = threading.currentThread()
while getattr(ct, "do_run", True):
ret = do_something(a)
returned_values.append(ret)
t1 = threading.Thread(target=function, args=("AAA",))
t2 = threading.Thread(target=function, args=("BBB",))
t3 = threading.Thread(target=function, args=("CCC",))
t1.start()
t2.start()
t3.start()
import time;time.sleep(10)
t1.do_run = t2.do_run = t3.do_run = False
编辑:忘了提,我使用Python 2.7
答
使用线程池
像这样的事情
from multiprocessing.pool import ThreadPool
pool = ThreadPool()
pool.map(function, list_containing_args)
P.S it works similar to multiprocess map.Each argument is given a new thread .You can specify the number of threads you want to spawn if you have limited resources or a big list
from multiprocessing.pool import ThreadPool
import subprocess
def func(ip):
c=subprocess.Popen("ping -c 3 "+ip, shell=True, stdout=subprocess.PIPE)
output, error= c.communicate()
return output
pool = ThreadPool()
for i in pool.map(func,["127.0.0.1", "www.google.com", "www.facebook.com"]):
print i
答
这里不会有ProcessPool
更适合,因为线程最适合网络I/O问题,其中ProcessPool
最适合用于内存密集型任务。
from concurrent.futures import ProcessPoolExecutor
with futures.ProcessPoolExecutor(max_workers=n) as executor:
executor.map(fn, args)
答
如果你坚持threading
,你可以做这样的:
-
设置你的论点提前
n_thread, args_set = 3, [('AAA',), ('BBB',), ('CCC',)]
-
存放在列表
所有实例threads = [threading.Thread(target=function, args=args_set[i]) for i in range(n_thread)] [t.start() for t in threads]
-
或者使用
t1
,t2
等。for i in range(n_thread): var_thread = locals()['t%d' % i] var_thread = threading.Thread(target=function, args=args_set[i]) var_thread.start() print t1, t2
的可能的复制[如何在Python中使用线程?](http://stackoverflow.com/questions/2846653/how-to-use-线程在Python中) – philshem