Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You haven't overridden the <code>run</code> method. There are two ways with processes (or threads) to have it execute code:</p> <ol> <li>Create a process specifying target</li> <li>Subclass the process, overriding the <code>run</code> method.</li> </ol> <p>Overriding <code>__init__</code> just means your process is all dressed up with nowhere to go. It should be used to give it attributes that it needs to perform what it needs to perform, but it shouldn't specify the task to be performed.</p> <p>In your code, all the heavy lifting is done in this line:</p> <pre><code>exec('worker'+str(i)+' = Worker(tmp)') </code></pre> <p>and nothing is done here:</p> <pre><code>exec('worker'+str(i)+'.start()') </code></pre> <p>So checking the results with <code>exec('print worker'+str(i)+'.result[0]')</code> should give you something meaningful, but only because the code you want to be executed <em>has</em> been executed, but on process construction, not on process start.</p> <p>Try this:</p> <pre><code>class Worker(Process): # example data transform def process(self, x): return (x * 2) / 3 def __init__(self, list): self.data = list self.result = [] super(Worker, self).__init__() def run(self): self.result = map(self.process, self.data) </code></pre> <p>EDIT:</p> <p>Okay... so I was just flying based on my threading instincts here, and they were all wrong. What we both didn't understand about processes is that you can't directly share variables. Whatever you pass to a new process to start is read, copied, and gone forever. Unless you use one of the two standard ways to share data: <a href="http://docs.python.org/library/multiprocessing.html#exchanging-objects-between-processes" rel="nofollow noreferrer">queues and pipes</a>. I've played around a little bit trying to get your code to work, but so far no luck. I think that will put you on the right track.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload