Threaded for loop

parallelpython at gmail.com parallelpython at gmail.com
Sat Jan 13 23:00:15 EST 2007


John wrote:
> I want to do something like this:
>
> for i = 1 in range(0,N):
>  for j = 1 in range(0,N):
>    D[i][j] = calculate(i,j)
>
> I would like to now do this using a fixed number of threads, say 10
> threads.
> What is the easiest way to do the "parfor" in python?
>
> Thanks in advance for your help,

As it was already mentioned before threads will not help in terms of
parallelism (only one thread will be actually working). If you want to
calculate this in parallel here is an easy solution:

import ppsmp

#start with 10 processes
srv = ppsmp.Server(10)

f = []

for i = 1 in range(0,N):
  for j = 1 in range(0,N):
      #it might be a little bit more complex if 'calculate' depends on
other modules or calls functions
      f.append(srv.submit(calculate, (i,j)))

for i = 1 in range(0,N):
  for j = 1 in range(0,N):
     D[i][j] = f.pop(0)

You can get the latest version of ppsmp module here:
http://www.parallelpython.com/




More information about the Python-list mailing list