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.
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
Post a Comment