Python threading/multiprocessing issue.

Brandon Harris brandon.harris at reelfx.com
Fri Jul 15 22:27:19 CEST 2011


I'm working on a tool that runs a number of process is separate thread.
I've, up to this point, been using threading.Thread, but from what I
read multiprocess will allow multiple processors to be used
 From the python docs on multiprocessing.
<Due to this, the multiprocessing module allows the programmer to fully
        leverage multiple processors on a given machine.>

I have run into an issue when modifying the thread object from the run
method. Threading.thread allows me to change an attribute in the run 
method and it hold while multiprocessing.Process loses it.

Here is an example illustrating the inconsistency that I've seen.

------------------------------------------------------------------------------
|
import time
import multiprocessing
import threading

def simple_process_call():
     my_process = SimpleProcess()
     my_process.start()
     while not my_process.done.is_set():
         pass

     print my_process.my_attribute

class SimpleProcess(multiprocessing.Process):
     def __init__(self):
         super(SimpleProcess, self).__init__()
         self.my_attribute = 'Fail'
         self.done = multiprocessing.Event()

     def run(self):
         self.my_attribute = 'Success'
         time.sleep(5)
         self.done.set()

def simple_thread_call():
     my_thread = SimpleThread()
     my_thread.start()
     while not my_thread.done.is_set():
         pass

     print my_thread.my_attribute

class SimpleThread(threading.Thread):
     def __init__(self):
         super(SimpleThread, self).__init__()
         self.my_attribute = 'Fail'
         self.done = threading.Event()

     def run(self):
         self.my_attribute = 'Success'
         time.sleep(5)
         self.done.set()

if __name__ == '__main__':
     # simple_process_call()
     simple_thread_call()|


------------------------------------------------------------------------------

The odd thing is that I can modify the multiprocessing.Event and it 
holds, but modifying any attribute on the class goes away.

If I am super ignorant of something, please cure me of it.

Thanks in advance!



Brandon L. Harris

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110715/840d3f19/attachment.html>


More information about the Python-list mailing list