title: Creating n processes for iterative task in python
body: <p>I have a complexed problem with python multiprocessing module.
  I have build a script that in one place has to call a multiargument function (call_function) for each element in a specyfic list. My idea is to define an integer 'N' and divide this problem for single sub processes.</p>
  
  <pre><code>li=[a,b,c,d,e] #elements are int's
  for element in li:
      call_function(element,string1,string2,int1)
  
  call_summary_function()
  </code></pre>
  
  <p>Summary function will analyze results obtained by all iterations of the loop. Now, I want each iteration to be carried out by single sub process, but there cannot be more than N subprocesses altogether. If so, main process should wait until 1 of subprocesses end and then perform another iteration. Also, call_sumary_function need to be called after all the sub processes finish.</p>
  
  <p>I have tried my best with multiprocessing module, Locks and global variables to keep the actual number of subprocesses running (to compare to N) but every time i get error.</p>
  
  <p>//--------------EDIT-------------//</p>
  
  <p>Firstly, the main process code:</p>
  
  <pre><code>MAX_PROCESSES=3
  lock=multiprocessing.Lock()
  processes=0
  k=0
  while k &lt; len(k_list):
  
      if processes&lt;=MAX_PROCESSES: # running processes &lt;= 'N' set by me
  
          p = multiprocessing.Process(target=single_analysis, args=(k_list[k],main_folder,training_testing,subsets,positive_name,ratio_list,lock,processes))
          p.start()
          k+=1
  
      else: time.sleep(1)
  
  
  while processes&gt;0: time.sleep(1)
  </code></pre>
  
  <p>Now: the function that is called by multiprocessing:</p>
  
  <pre><code>def single_analysis(k,main_folder,training_testing,subsets,positive_name,ratio_list,lock,processes):
  
  lock.acquire()
  processes+=1
  lock.release()
  
  #stuff to do
  
  lock.acquire()
  processes-=1
  lock.release()
  </code></pre>
  
  <p>I get the Error that int value (processes variable) is always equal to 0, since <code>single_analysis()</code> function seems to create new, local variable <code>processes</code>.
  When I change processes to global and import it in <code>single_analysis()</code> with <code>global</code> keyword and type <code>print processes in</code> within the function I get <code>len(li)</code> times 1... </p>