繁体   English   中英

Python 中在不同线程中调用相同函数的最佳方法是什么?

[英]What is the best way in Python to call the same function in separate threads?

在不同的线程中调用相同的函数并有一个单独的列表和每个实例的返回值的最佳方法是什么,而不复制函数

例子:

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)

PS 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 ,你可以这样做:

  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

暂无
暂无

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

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