run a function in another processor in python

Astan Chee astan.chee at gmail.com
Fri Dec 10 07:08:06 EST 2010


I just saw this:
http://bugs.python.org/issue8094
which seem to be similar to what I'm having. Does anyone know if there is a
fix for it?
Thanks again

On Fri, Dec 10, 2010 at 11:02 PM, Astan Chee <astan.chee at gmail.com> wrote:

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


More information about the Python-list mailing list