I have 2 simple functions(loops over a range) that can run separately without any dependency.. I'm trying to run this 2 functions both using the Python multiprocessing module as well as multithreading module..
When I compared the output, I see the multiprocess application takes 1 second more than the multi-threading module..
I read multi-threading is not that efficient because of the Global interpreter lock...
Based on the above statements -
1. Is is best to use the multiprocessing if there is no dependency between 2 processes?
2. How to calculate the number of processes/threads that I can run in my machine for maximum efficiency..
3. Also, is there a way to calculate the efficiency of the program by using multithreading...
Multithread module...
from multiprocessing import Process import thread import platform import os import time import threading class Thread1(threading.Thread): def __init__(self,threadindicator): threading.Thread.__init__(self) self.threadind = threadindicator def run(self): starttime = time.time() if self.threadind == 'A': process1() else: process2() endtime = time.time() print 'Thread 1 complete : Time Taken = ', endtime - starttime def process1(): starttime = time.time() for i in range(100000): for j in range(10000): pass endtime = time.time() def process2(): for i in range(1000): for j in range(1000): pass def main(): print 'Main Thread' starttime = time.time() thread1 = Thread1('A') thread2 = Thread1('B') thread1.start() thread2.start() threads = [] threads.append(thread1) threads.append(thread2) for t in threads: t.join() endtime = time.time() print 'Main Thread Complete , Total Time Taken = ', endtime - starttime if __name__ == '__main__': main() multiprocess module
from multiprocessing import Process import platform import os import time def process1(): # print 'process_1 processor =',platform.processor() starttime = time.time() for i in range(100000): for j in range(10000): pass endtime = time.time() print 'Process 1 complete : Time Taken = ', endtime - starttime def process2(): # print 'process_2 processor =',platform.processor() starttime = time.time() for i in range(1000): for j in range(1000): pass endtime = time.time() print 'Process 2 complete : Time Taken = ', endtime - starttime def main(): print 'Main Process start' starttime = time.time() processlist = [] p1 = Process(target=process1) p1.start() processlist.append(p1) p2 = Process(target = process2) p2.start() processlist.append(p2) for i in processlist: i.join() endtime = time.time() print 'Main Process Complete - Total time taken = ', endtime - starttime if __name__ == '__main__': main()