Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are many reason why it is not working.</p> <p>First, in the function <code>read_occupation</code>, you are changing the binding of a local variable. The original object is left untouched. On the contrary, in <code>get_p_values</code>, you are modifying the object (the <code>[]</code> operator call the <code>__setitem__</code> function of the object that change the internal representation). A better idea in this case would be to use a proper object.</p> <p>If you want to change the whole content of the list in <code>read_occupation</code>, you can use the list splicing operator to assign to the whole list:</p> <pre><code>def read_occupation(matrix): matrix[:] = [ [1, 2, 3] ] </code></pre> <p>BTW, if you call you function <code>read_occupation</code> the caller will probably expect it not to change its parameter. You should probably rename it <code>update_occupation</code> or something like that.</p> <p>Second, when you create your variables, via multiplication, you get a <code>list</code> where every index contains a reference to the same item. The code <code>p = [ [0] ] * 3</code> is equivalent to:</p> <pre><code>&gt;&gt;&gt; l = [0] # Naming the list for more clarity &gt;&gt;&gt; p = [ l, l, l ] # Each index points to the same list `l` </code></pre> <p>Third, the <code>Process</code> class expect a <code>tuple</code> (or in fact an object following the iterable protocol) for its <code>args</code> parameter. You are not passing a tuple, but a single item that happens to be a <code>list</code> of one <code>int</code> (which is why you get a <code>TypeError</code> exception). You need to use the following syntax to pass a <code>tuple</code>:</p> <pre><code># Please note the comma after the closing bracket, it is important p1 = Process( target=read_occupation, args=( matrix[ index ], ) ) # In python, the syntax for a tuple is weird for tuple of size &lt; 2 # . tuple of 0 item: () # . tuple of 1 item: (1,) # . tuple of 2 items: (1, 2) </code></pre> <p>Fourth, you use <code>multiprocessing.Process</code> that spawn a new process in which your code is executed. There is no communication back in your code (I don't know if it is possible to communicate back from this process to the original one). So the variable in the original code is not modified.</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