Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this case, you probably want to define a simple function to perform the calculation and get <code>localResult</code>.</p> <pre><code>def getLocalResult(args): """ Do whatever you want in this func. The point is that it takes x,i,j and returns localResult """ x,i,j = args #unpack args return doSomething(x,i,j) </code></pre> <p>Now in your computation function, you just create a pool of workers and map the local results:</p> <pre><code>import multiprocessing def computation(np=4): """ np is number of processes to fork """ p = multiprocessing.Pool(np) output = p.map(getLocalResults, [(x,i,j) for x in range(i,j)] ) return output </code></pre> <p>I've removed the global here because it's unnecessary (globals are usually unnecessary). In your calling routine you should just do <code>output.extend(computation(np=4))</code> or something similar.</p> <p><strong>EDIT</strong></p> <p>Here's a "working" example of your code:</p> <pre><code>from multiprocessing import Pool def computation(args): length, startPosition, npoints = args print(args) length = 100 np=4 p = Pool(processes=np) p.map(computation, [(startPosition,startPosition+length//np, length//np) for startPosition in range(0, length, length//np)]) </code></pre> <p>Note that what you had didn't work because you were using an instance method as your function. multiprocessing starts new processes and sends the information between processes via <code>pickle</code>, therefore, only objects which can be pickled can be used. Note that it really doesn't make sense to use an instance method anyway. Each process is a copy of the parent, so any changes to state which happen in the processes do not propagate back to the parent anyway. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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