Threads vs Processes

Nick Craig-Wood nick at craig-wood.com
Thu Jul 27 05:30:03 EDT 2006


bryanjugglercryptographer at yahoo.com <bryanjugglercryptographer at yahoo.com> wrote:
>  Yes, someone can, and that someone might as well be you.
>  How long does it take to create and clean up 100 trivial
>  processes on your system? How about 100 threads? What
>  portion of your user waiting time is that?

Here is test prog...

The results are on my 2.6GHz P4 linux system

  Forking
  1000 loops, best of 3: 546 usec per loop
  Threading
  10000 loops, best of 3: 199 usec per loop

Indicating that starting up and tearing down new threads is 2.5 times
quicker than starting new processes under python.

This is probably irrelevant in the real world though!


"""
Time threads vs fork
"""

import os
import timeit
import threading

def do_child_stuff():
    """Trivial function for children to run"""
    # print "hello from child"
    pass

def fork_test():
    """Test forking"""
    pid = os.fork()
    if pid == 0:
        # child
        do_child_stuff()
        os._exit(0)
    # parent - wait for child to finish
    os.waitpid(pid, os.P_WAIT)

def thread_test():
    """Test threading"""
    t = threading.Thread(target=do_child_stuff)
    t.start()
    # wait for child to finish
    t.join()
    
def main():
    print "Forking"
    timeit.main(["-s", "from __main__ import fork_test", "fork_test()"])
    print "Threading"
    timeit.main(["-s", "from __main__ import thread_test", "thread_test()"])
    
if __name__ == "__main__":
    main()

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list