开发者

Python Threading Over a Counter

I have a function that needs to be called N 开发者_Python百科times. I would like T threads to execute the function in parallel. How would you model this in python?


And now, for some alternative fun :)

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))

Output:

% 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]


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)


The problem of Python is that it not supports OS-based threads because of the famous GIL (see http://jessenoller.com/2009/02/01/python-threads-and-the-global-interpreter-lock/). The easiest method to use real threading (for example n threads) in my opinion is to use parallel python (see http://www.parallelpython.com/) in combination with the parallel map version for parallel python (http://honeypot.net/yet-another-python-map). This can be used as follows:

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

import pp
import ppmap

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


for t in threadpool:
   t.execute(function)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜