Parallel Processing

Dave Angel d at
Sun Jan 8 09:34:53 EST 2012

On 01/08/2012 08:23 AM, Yigit Turgut wrote:
> Hi all,
> I am trying to run two functions at the same time with Parallel
> Processing (pp) as following ;
> import pygame
> import sys
> import time
> import math
> import pp
> screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
> timer = pygame.time.Clock()
> white = True
> start = time.time()
> end = time.time() - start
> def test1():
>    global end
>    while(end<5):
>      end = time.time() - start
>      timer.tick(4) #FPS
>      screen.fill((255,255,255) if white else (0, 0, 0))
>      white = not white
>      pygame.display.update()
> def test2():
>    global end
>    while(end2<5):
>      end2 = time.time() - start
>      print end
> ppservers = ()
> if len(sys.argv)>  1:
>      ncpus = int(sys.argv[1])
>      # Creates jobserver with ncpus workers
>      job_server = pp.Server(ncpus, ppservers=ppservers)
> else:
>      # Creates jobserver with automatically detected number of workers
>      job_server = pp.Server(ppservers=ppservers)
> print "Starting PP with", job_server.get_ncpus(), "workers"
> job1 = job_server.submit(test1,test2)
> result = job1()
> And the output is ;
> Starting PP with 2 workers
> Traceback (most recent call last):
>    File "", line 46, in<module>
>      job1 = job_server.submit(test1,test2)
>    File "/usr/lib/python2.6/site-packages/", line 402, in submit
>      raise TypeError("args argument must be a tuple")
> TypeError: args argument must be a tuple
> When I change job1 to just to see if it will run one function only;
> job1 = job_server.submit(test1)
> I get an output of;
> NameError: global name 'end' is not defined
> end variable is defined as global but I get a NameError. Anyone has an
> idea what's going on ?
First, please tell us about any external dependencies (eg. imports).  
Pygame may not matter, since there are many people here using it, but 
each of us has to hunt down something that does parallel processing with 
an interface similar to what you're using.  For the next person, the 
most likely candidate out of the dozens available is:

Your problem, as stated in the raise statement, is that the submit 
method is expecting a tuple for its "args argument".  Now I had to go to 
the website to find an example, but it appears that your second argument 
should have been a tuple of the arguments for your function.  See
*      submit*(self, func, args=(), depfuncs=(), modules=(), 
callback=None, callbackargs=(), group='default', globals=None)

on page:

So you're passing it two function objects, and the second one is in the 
place where it's expecting a tuple of arguments.  Apparently you should 
be calling submit multiple times, once for each function.

As for the error you get when you do that, please post the full 
traceback. I suspect that the message you did post has a typo in it, or 
that the error occurred when your source code was not as you show it 
here.  You have two variables end and end2, and only the first one is 
ever declared global.  func2() should get a different error when you run 
it; since it tries to use a local end2 before defining it.

I recommend always testing your code with a single thread before trying 
to launch more complex multithread stuff.  Just call func() and func2(), 
and see if they complete, and get reasonable answers.



More information about the Python-list mailing list