There doesn't seem to be any good examples on POSH or it's not clear to me. For when using with a for loop like mk is doing who started this thread. How would somethings like this be possible to do with POSH? The example show how to share variables between processes/threads but nothing about How the thread starts or a for loop.<br>
<br clear="all">-Alex Goretoy<br><a href="http://www.alexgoretoy.com">http://www.alexgoretoy.com</a><br><a href="mailto:somebodywhocarez@gmail.com">somebodywhocarez@gmail.com</a><br>
<br><br><div class="gmail_quote">On Sat, Jan 3, 2009 at 1:31 PM, Nick Craig-Wood <span dir="ltr"><<a href="mailto:nick@craig-wood.com">nick@craig-wood.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">mk <<a href="mailto:mrkafk@gmail.com">mrkafk@gmail.com</a>> wrote:<br>
</div>>  After reading <a href="http://www.python.org/dev/peps/pep-0371/" target="_blank">http://www.python.org/dev/peps/pep-0371/</a> I was under<br>
<div class="Ih2E3d">>  impression that performance of multiprocessing package is similar to<br>
>  that of thread / threading. However, to familiarize myself with both<br>
>  packages I wrote my own test of spawning and returning 100,000 empty<br>
>  threads or processes (while maintaining at most 100 processes / threads<br>
>  active at any one time), respectively.<br>
><br>
</div><div class="Ih2E3d">>  The results I got are very different from the benchmark quoted in PEP<br>
>  371. On twin Xeon machine the threaded version executed in 5.54 secs,<br>
>  while multiprocessing version took over 222 secs to complete!<br>
><br>
</div><div class="Ih2E3d">>  Am I doing smth wrong in code below?<br>
<br>
</div>Yes!<br>
<br>
The problem with your code is that you never start more than one<br>
process at once in the multiprocessing example.  Just check ps when it<br>
is running and you will see.<br>
<br>
My conjecture is that this is due to the way fork() works under unix.<br>
I think that when the parent forks it yields the CPU to the child.<br>
Because you are giving the child effectively no work to do it returns<br>
immediately, re-awakening the parent, thus serialising your jobs.<br>
<br>
If you give the children some work to do you'll see a quite different<br>
result.  I gave each child time.sleep(1) to do and cut down the total<br>
number to 10,000.<br>
<br>
$ ./test_multiprocessing.py<br>
== Process 1000 working ==<br>
== Process 2000 working ==<br>
== Process 3000 working ==<br>
== Process 4000 working ==<br>
== Process 5000 working ==<br>
== Process 6000 working ==<br>
== Process 7000 working ==<br>
== Process 8000 working ==<br>
== Process 9000 working ==<br>
== Process 10000 working ==<br>
<div class="Ih2E3d">=== Main thread waiting for all processes to finish ===<br>
</div>Total time: 101.382129192<br>
<br>
$ ./test_threading.py<br>
== Thread 1000 working ==<br>
== Thread 2000 working ==<br>
== Thread 3000 working ==<br>
== Thread 4000 working ==<br>
== Thread 5000 working ==<br>
== Thread 6000 working ==<br>
== Thread 7000 working ==<br>
== Thread 8000 working ==<br>
== Thread 9000 working ==<br>
== Thread 10000 working ==<br>
Total time:  100.659118176<br>
<br>
So almost identical results and as expected - we ran 10,000 sleep(1)s<br>
in 100 seconds so we must have been running 100 simultaneously.<br>
<br>
If you replace the "time.sleep(1)" with "for _ in xrange(1000000):<br>
pass" you get this much more interesting answer on my dual core linux<br>
laptop, showing nicely the effect of the contention on the python<br>
global interpreter lock and how multiprocessing avoids it.<br>
<br>
$ ./test_multiprocessing.py<br>
== Process 1000 working ==<br>
== Process 2000 working ==<br>
== Process 3000 working ==<br>
== Process 4000 working ==<br>
== Process 5000 working ==<br>
== Process 6000 working ==<br>
== Process 7000 working ==<br>
== Process 8000 working ==<br>
== Process 9000 working ==<br>
== Process 10000 working ==<br>
<div class="Ih2E3d">=== Main thread waiting for all processes to finish ===<br>
</div>Total time: 266.808327913<br>
<br>
$ ./test_threading.py<br>
== Thread 1000 working ==<br>
== Thread 2000 working ==<br>
== Thread 3000 working ==<br>
== Thread 4000 working ==<br>
== Thread 5000 working ==<br>
== Thread 6000 working ==<br>
== Thread 7000 working ==<br>
== Thread 8000 working ==<br>
== Thread 9000 working ==<br>
== Thread 10000 working ==<br>
Total time:  834.81882<br>
<font color="#888888"><br>
--<br>
Nick Craig-Wood <<a href="mailto:nick@craig-wood.com">nick@craig-wood.com</a>> -- <a href="http://www.craig-wood.com/nick" target="_blank">http://www.craig-wood.com/nick</a><br>
</font><div><div></div><div class="Wj3C7c">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>