<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"><meta http-equiv="Content-Type" content="text/html charset=iso-8859-1"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Nov 2, 2013, at 11:44 AM, Sherard Hall <<a href="mailto:smhall05@gmail.com">smhall05@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><p dir="ltr">Thank you for the response. Processing time is very important so I suspect having to write to disk will take more time than letting the other processes complete without finding the answer. So I did some profiling one process finds the answer in about 250ms, but since I can't stop the other processes, it takes about 800ms before I can use the answer.  Do you recommend a global variable flag? Any other suggestions? </p>

<div class="gmail_quote">On Nov 2, 2013 8:17 AM, "William Ray Wing" <<a href="mailto:wrw@mac.com">wrw@mac.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin: 0px 0px 0px 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; position: static; z-index: auto; ">
On Nov 2, 2013, at 1:03 AM, smhall05 <<a href="mailto:smhall05@gmail.com">smhall05@gmail.com</a>> wrote:<br>
<br>
> On Friday, November 1, 2013 10:52:40 PM UTC-4, MRAB wrote:<br>
>> On 02/11/2013 02:35, smhall05 wrote:<br>
>><br>
>>> I am using a basic multiprocessing snippet I found:<br>
>>><br>
>>> #-----------------------------------------------------<br>
>>> from multiprocessing import Pool<br>
>>><br>
>>> def  f(x):<br>
>>>     return x*x<br>
>>><br>
>>> if __name__ == '__main__':<br>
>>>     pool = Pool(processes=4)              # start 4 worker processes<br>
>>>     result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously<br>
>>>     print result.get(timeout=1)<br>
>>>     print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"<br>
>>> #---------------------------------------------------------<br>
>>><br>
>>> I am using this code to have each process go off and solve the same problem, just with different inputs to the problem. I need to be able to kill all processes once 1 of n processes has come up with the solution. There will only be one answer.<br>

>>><br>
>>> I have tried:<br>
>>><br>
>>> sys.exit(0) #this causes the program to hang<br>
>>> pool.close()<br>
>>> pool.terminate<br>
>>><br>
>><br>
>> Did you actually mean "pool.terminate", or is that a typo for<br>
>><br>
>> "pool.terminate()"?<br>
>><br>
>>> These still allow further processing before the program terminates. What else can I try? I am not able to share the exact code at this time. I can provide more detail if I am unclear. Thank you<br>
>>><br>
><br>
> I am not sure to be honest, however it turns out that I can't use pool.terminate() because pool is defined in main and not accessible under my def in which I check for the correct answer.<br>
> --<br>
> <a href="https://mail.python.org/mailman/listinfo/python-list" target="_blank">https://mail.python.org/mailman/listinfo/python-list</a><br>
<br>
So, the simplest solution to that situation is to have whichever subprocess that finds the correct answer set a flag which the calling process can check.  Depending on your OS, that flag can be anything from setting a lock to something as simple as creating a file which the calling process periodically wakes up and looks for, maybe just a file in which the subprocess has written the answer.<br>

<br>
Bill<br>
<br>
</blockquote></div>
-- <br><a href="https://mail.python.org/mailman/listinfo/python-list">https://mail.python.org/mailman/listinfo/python-list</a><br></blockquote></div><br><div>Well, the multiprocessing library provides listeners and clients that wrap BSD style sockets and allow you to send (push) arbitrary python objects to a listener, i.e., the master.  There might be something better that was OS specific, but this will keep it pure python.  I've not tested it, but there is a simple example here on Stackoverflow: </div><div><br></div><div><a href="http://stackoverflow.com/questions/6920858/interprocess-communication-in-python">http://stackoverflow.com/questions/6920858/interprocess-communication-in-python</a></div><div><br></div><div>-Bill</div></body></html>