Python 中在不同线程中调用相同函数的最佳方法是什么?
What is the best way in Python to call the same function in separate threads?
在不同的线程中调用相同的 function 并为每个实例创建一个包含返回值的单独列表而不重复 function[=20] 的最佳方法是什么=]?
示例:
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
在不同的线程中调用相同的 function 并为每个实例创建一个包含返回值的单独列表而不重复 function[=20] 的最佳方法是什么=]?
示例:
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