<div>Thanks for that. I'll try and see if it makes any difference but I'm using python 2.6 not 3</div>
<div>Are the multiprocessing modules different? That code (or whatever is using the multiprocessing module) seems to cause infinite python processes on my machine and eventually kills it.</div>
<div>I'm running python 2.6 on windows 7 x64 ....is it the ammount of memory/cpu speed/number of cpu that is the issue?</div>
<div>Thanks for any clarification</div>
<div><br><br> </div>
<div class="gmail_quote">On Fri, Dec 10, 2010 at 4:16 AM, geremy condra <span dir="ltr"><<a href="mailto:debatem1@gmail.com" target="_blank">debatem1@gmail.com</a>></span> wrote:<br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">
<div>
<div></div>
<div>On Thu, Dec 9, 2010 at 5:03 AM, Astan Chee <<a href="mailto:astan.chee@gmail.com" target="_blank">astan.chee@gmail.com</a>> wrote:<br>> Thanks but I'm having trouble with that module too. Currently what I<br>
> have is something like this:<br>><br>> import sys<br>> import os<br>> import multiprocessing<br>><br>> import time<br>><br>> def functionTester(num):<br>> return ((num+2)/(num-2))**2<br>
><br>> num_args = [61,62,33,7,12,16,19,35,36,37,38,55,56,57,63]<br>><br>> max_result = 0<br>><br>> start = time.time()<br>><br>> num_processes = multiprocessing.cpu_count()<br>><br>> threads = []<br>
> len_stas = len(num_args)<br>><br>> for list_item in num_args:<br>> if len(threads) < num_processes:<br>> p = multiprocessing.Process(target=functionTester,args=[list_item])<br>> p.start()<br>
> print p, p.is_alive()<br>> threads.append(p)<br>> else:<br>> for thread in threads:<br>> if not thread.is_alive():<br>> threads.remove(thread)<br>><br>
> print "Result " + str(max_result)<br>> end = time.time()<br>> elapsed= end - start<br>> print "Took", elapsed, "seconds to execute"<br>><br>> But it doesn't give me any return data. It also spawns an infinite<br>
> number of (sub)processes that crashes my machine. What am I doing<br>> wrong here?<br>><br>> On 12/9/10, Jean-Michel Pichavant <<a href="mailto:jeanmichel@sequans.com" target="_blank">jeanmichel@sequans.com</a>> wrote:<br>
>> Astan Chee wrote:<br>>>> Hi,<br>>>> I've got a python script that calls a function many times with various<br>>>> arguments and returns a result. What I'm trying to do is run this<br>
>>> function each on different processors and compile the result at the<br>>>> end based on the function result. The script looks something like<br>>>> this:<br>>>><br>>>><br>>>> import time<br>
>>><br>>>> def functionTester(num):<br>>>> return ((num+2)/(num-2))**2<br>>>><br>>>> num_args = [1,2,3,7,12,16,19,35,36,37,38,55,56,57,63,44,71,81,91]<br>>>><br>>>> max_result = 0<br>
>>><br>>>> start = time.time()<br>>>><br>>>> for n in num_args:<br>>>> result = functionTester(n)<br>>>> if result > max_result:<br>>>> max_result = result<br>
>>><br>>>> print "Result " + str(max_result)<br>>>> end = time.time()<br>>>> elapsed= end - start<br>>>> print "Took", elapsed, "seconds to execute"<br>
>>><br>>>><br>>>> What I'm trying to do is run each function on a processor and when its<br>>>> done, move on to the next function-argument specifically on windows 7<br>>>> x64 using python 2.6. How do I do this?<br>
>>> Thanks for any help<br>>>><br>>> If I'm not wrong, CPU management is handled by your system, meaning<br>>> there's no way to 'force' anything to run on a specific CPU. However,<br>
>> you may try to execute your fonction in a subprocess, so that the system<br>>> will use different CPUs (hopefully). You then just need to limit the<br>>> number of subprocess alive at the same time.<br>
>><br>>> Have a look here<br>>> <a href="http://docs.python.org/library/multiprocessing.html" target="_blank">http://docs.python.org/library/multiprocessing.html</a><br>>><br>>> JM<br>>><br>
</div></div>> --<br>
<div>> <a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>><br><br></div>Here's a way of doing what I think you mean to do. I assume that<br>
max_result should be the maximum value returned by a run of<br>functionTester.<br><br>Also, just a note, usually function names like this are spelled<br>function_tester rather than functionTester in python.<br><br>If you need to de-python3ify it just change the line at the top and<br>
cast your nums to floats, otherwise you'll get integer division (which<br>I again assume you don't want).<br><br>#! /usr/bin/env python3<br><br>import time<br>import multiprocessing<br>
<div><br><br>def functionTester(num):<br> return ((num+2)/(num-2))**2<br><br>num_args = [61,62,33,7,12,16,19,35,36,37,38,55,56,57,63]<br><br></div>
<div>num_processes = multiprocessing.cpu_count()<br></div>pool = multiprocessing.Pool(num_processes)<br><br>start = time.time()<br>results = pool.map(functionTester, num_args)<br>end = time.time()<br><br># is this what you meant to do with the results?<br>
max_result = max(results)<br>print("Result " + str(max_result))<br><br>elapsed = end - start<br>print("Took", elapsed, "seconds to execute")<br><font color="#888888"><br>Geremy Condra<br></font></blockquote>
</div><br>