什么是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

+0

的可能的复制[如何在Python中使用线程?](http://stackoverflow.com/questions/2846653/how-to-use-线程在Python中) – philshem

使用线程池

像这样的事情

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 
+0

谢谢。你能否解释一下这是如何工作的? – rbs

+0

是否有一个原因,我会用,而不是multiprocessing.Pool类线程池? – rbs

+1

@rbs线程池将推出'threads'这是比较容易和便宜,如果你正在做'IO'相关的工作。如果你用'multiprocess'将推出'Processes'具有overheads.So如果你的任务涉及'IO'使用'threads'别的'processes' – vks

这里不会有ProcessPool更适合,因为线程最适合网络I/O问题,其中ProcessPool最适合用于内存密集型任务。

from concurrent.futures import ProcessPoolExecutor 

with futures.ProcessPoolExecutor(max_workers=n) as executor: 
    executor.map(fn, args) 

如果你坚持threading,你可以做这样的:

  1. 设置你的论点提前

    n_thread, args_set = 3, [('AAA',), ('BBB',), ('CCC',)] 
    
  2. 存放在列表

    threads = [threading.Thread(target=function, args=args_set[i]) 
          for i in range(n_thread)] 
    [t.start() for t in threads] 
    
    所有实例
  3. 或者使用t1t2等。

    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