Is multiprocessing possible in Python?

If your machine has a single processor you can use the module named threading for python to spawn multiple threads on single core.

If your machine has more than one processors which is the case mostly these days, then due to global interpreter lock, threading module will not work for you. In this case you have to use another module named multiprocessing.

Here are simple examples of multiprocessing module from PyMOTW


import multiprocessing
import time
import sys

def daemon():
    print 'Starting:', multiprocessing.current_process().name
    time.sleep(2)
    print 'Exiting :', multiprocessing.current_process().name

def non_daemon():
    print 'Starting:', multiprocessing.current_process().name
    print 'Exiting :', multiprocessing.current_process().name

if __name__ == '__main__':
    d = multiprocessing.Process(name='daemon', target=daemon)
    d.daemon = True

    n = multiprocessing.Process(name='non-daemon', target=non_daemon)
    n.daemon = False

    d.start()
    n.start()

    d.join(1)
    print 'd.is_alive()', d.is_alive()
    n.join()

Comments

Popular posts from this blog

What is Skylake and how does it compare with Broadwell or Haswell?

What is Tokenization?

What is three in a box model?