Wonder if you can help? I'm new to Python, and am trying to put together a simple piece of code that calculates prime numbers in parallel (across multiple CPU cores)... I know it's lame, but it seemed like a reasonable piece of code to learn about Python threads etc.
I have successfully managed to run the code using Threads, but am now investigating Process(es).
The code below runs fine up to the point where i'm gathering the results. I've tried to debug using eclipse & pdev debugger and have found the While loop that i'm using to remove items from outputQueue seems to stick after 3 iterations, so the code never actually reaches the stage where it outputs the results.
Any ideas / advice / help would be greatly appreciated.
Many thanks
Craig ------ code
from multiprocessing import Process, Queue
# return true if number is prime, else false
def calcPrime(number):
divisor=2
if number%2==0:
return False
numberIsPrime=True
while divisor*divisor <= number:
if number % divisor == 0:
numberIsPrime = False
break
divisor = divisor + 1
return numberIsPrime
# generate list of primes
def generatePrimes(minimum, maximum):
return [num for num in range(minimum,maximum) if calcPrime(num)==True]
def workerThread(output, mn, mx):
primelist=generatePrimes(mn,mx)
output.put(primelist)
def main():
outputQueue=Queue()
t=[]
t.append(Process(target=workerThread, args=(outputQueue, 1,25000)))
t.append(Process(target=workerThread, args=(outputQueue, 25001, 50000)))
t.append(Process(target=workerThread, args=(outputQueue, 50001, 75000)))
t.append(Process(target=workerThread, args=(outputQueue, 75001, 100000)))
#start all threads
for idx in range(len(t)):
t[idx].daemon=True
t[idx].start()
#wait for all process threads to complete
for p in t:
p.join()
print("Processes finished")
# gather all results
l=[]
while True:
try:
l+=outputQueue.get() # Code seems to stick here after about 3-4 iterations
except:
break
#print out our lovely primes
for idx in range(len(l)):
print (str(l[idx]))
# standard code
if __name__ == '__main__':
main()
if calcPrime(num)==TrueThe comparison is redundant. Simply useif calcPrime(num). Also, when comparing forTrue,FalseorNoneyou should theisoperator:if calcPrime(num) is True. Whenever you simply want to know the "truthy" value of something simply usenotto check for "false" and the object itself for "true".