Python穿越柜台

时间:2011-03-17 18:16:48

标签: python

我有一个需要被调用N次的函数。我想T线程并行执行该函数。你会如何在python中对此进行建模?

4 个答案:

答案 0 :(得分:3)

现在,为了另一种乐趣:)

import threading
from itertools import repeat
from multiprocessing.pool import ThreadPool   # this is a THREAD POOL! undocumented :)


def execute_me():
    print threading.current_thread().name, 'python is fun'


tp = ThreadPool(processes=4)
print tp.map(lambda x: x(), repeat(execute_me, 4))

输出:

% python mpthreadpool.py 
Thread-1 python is fun
 Thread-2 python is fun
Thread-3 python is fun
Thread-3 python is fun
[None, None, None, None]

答案 1 :(得分:1)

threads = []
for i in range(NUM_THREADS):
    t = threading.Thread(target=your_function, args=your_argslist)
    t.start()  # if you want to start it immediately, otherwise you can defer it 
    threads.append(t)

答案 2 :(得分:1)

Python的问题在于它不支持基于操作系统的线程,因为着名的GIL(参见http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/)。在我看来,使用真正线程(例如n个线程)的最简单方法是使用并行python(参见http://www.parallelpython.com/)结合并行映射版本的并行python(http://honeypot.net/yet-另一个-蟒-MAP)。这可以使用如下:

def func(arg1):
   # do something with arg1
   return result

import pp
import ppmap

ppmap.ppmap(n, func, [test1, ... test2])

答案 3 :(得分:0)

for t in threadpool:
   t.execute(function)