The difference of gevent and multiprocess
<p>I am learning how to make my script run faster. I think parallel is a good way. So I try gevent and multiprocessing. But I am confused by it's different result. Let me show two examples I met,</p>

<p>ex 1:</p>

<pre><code>a=np.zeros([3])
def f(i):
    a[i]=1
    print a
def f_para():
    p=multiprocessing.Pool()
    p.map(f, range(3))

def f_asy():
    threads = [gevent.spawn(f, i) for i in xrange(3)]
    gevent.joinall(threads)

f_para()
[ 0.  1.  0.]
[ 0.  0.  1.]
[ 1.  0.  0.]

f_asy()
[ 1.  0.  0.]
[ 1.  1.  0.]
[ 1.  1.  1.]
</code></pre>

<p>I find that using multiprocessing, the global object <code>a</code> never change in fat, and after running <code>f_para()</code>, <code>a</code> is still the original array. While running <code>f_asy()</code>, it's different, <code>a</code> changed.</p>

<p>ex 2:</p>

<pre><code>def f2(i):
    subprocess.call(['./a.out', str(i)])
    time.sleep(0.2)

def f2_loop():
    for i in xrange(20):
        f2(i)

def f2_para():
    p=multiprocessing.Pool()
    p.map(f2, range(20))

def f2_asy():
    threads = [gevent.spawn(f2, i) for i in xrange(20)]
    gevent.joinall(threads)

%timeit -n1 f2_loop()
1 loop, best of 3: 4.22 s per loop
%timeit -n1 f2_asy()
1 loop, best of 3: 4.22 s per loop
%timeit -n1 f2_para()
1 loop, best of 3: 657 ms per loop
</code></pre>

<p>I find that <code>f2_asy()</code> don't decrease run time. And the output of <code>f2_asy()</code> is one by one, just as <code>f2_loop()</code>, so there is no parallel in <code>f2_asy()</code> I think. </p>

<p>The <code>a.out</code> is a simple c++ code:</p>

<pre><code> #include &lt;iostream&gt;

 int main(int argc, char* argv[])
 {
   std::cout&lt;&lt;argv[1]&lt;&lt;std::endl;
   return 0;
 }
</code></pre>

<p>So my question is that:</p>

<ol>
<li><p>why in ex 1, <code>f_para</code> can change the value of global array <code>a</code> ?</p></li>
<li><p>why in ex 2, <code>f2_asy</code> can't do parallel?</p></li>
</ol>

<p>Dose any knows the difference between gevent and multiprocessing? I am very grateful if you are willing to explain it.</p>

<p>ex1:</p>

<p>when you use multiprocess each process has separate memory(unlike threads)</p>

<p>ex2:</p>

<p>gevent does not create threads, it creates Greenlets(coroutines)!</p>

<blockquote>
  <p>Greenlets all run inside of the OS process for the main program but are scheduled cooperatively.</p>
  
  <p><strong>Only one greenlet is ever running at any given time.</strong></p>
  
  <p>This differs from any of the real parallelism constructs provided by multiprocessing or threading libraries which do spin processes and POSIX threads which are scheduled by the operating system and are truly parallel.</p>
</blockquote>

python python-2.7 parallel-processing multiprocessing gevent