While learning Python's threading module I've run a simple test. Interesting that the threads are running sequentially and not parallel. Is it possible to modify this test code so a program executes the threads in same fashion as multiprocessing does: in parallel?
import threadingdef mySlowFunc(arg):print "\nStarting...", argm=0for i in range(arg):m+=iprint '\n...Finishing', argmyList =[35000000, 45000000, 55000000]for each in myList:thread = threading.Thread(target=mySlowFunc, args=(each,) )thread.daemon = Truethread.start()thread.join()print "\n Happy End \n"
REVISED CODE:
This version of the code will initiate 6 'threads' running in 'parallel'. But even while there will be 6 threads only two CPU's threads are actually used (6 other Physical CPU threads will be idling and doing nothing).
import threadingdef mySlowFunc(arg):print "\nStarting " + str(arg) + "..."m=0for i in range(arg):m+=iprint '\n...Finishing ' + str(arg)myList =[35000000, 45000000, 55000000, 25000000, 75000000, 65000000]for each in myList:thread = threading.Thread(target=mySlowFunc, args=(each,) )thread.daemon = Falsethread.start()print "\n Bottom of script reached \n"