this is probably a pretty dumb question..
I was fooling around some in python and decided to make two version of prime number apps, one that just indefinately counts up all prime-numbers from start until you fry ur pc and one that lets the user input a number and then checks if the number is a prime number or not.
So i made one that worked fine but it was quite slow. took 41 sec to check if 269996535 was a prime. i guess my algorithm is pretty bad.. but thats not my concern! i then read up on multiprocessing (multithreading) and decided to give it a go, after some minutes of tinkering i got it to work on smaller numbers and it was fast, so i decided to compare with the previously big number i had (269996535)
and i looked at my resmon and the cpu spiked, then the music stopped, all my programs started to crash one by one and then finally bluescreen.
Can someone explain why this happened and why i cant get this to work. im not looking for a better algorithm im just simply trying to figure out why my pc crashed when trying to multithread.
code that works
import time
def prime(x):
start = time.time()
num = x+1
range_num = []
two = x/2
five = x/5
ten = x/10
if not two.is_integer() or five.is_integer() or ten.is_integer():
for i in range(1, num):
y = x/i
if y.is_integer():
range_num.append(True)
else:
range_num.append(False)
total = 0
for ele in range(0, len(range_num)):
total = total + range_num[ele]
if num == 1:
print(1, " is a prime number")
elif total == 2:
print(num-1, " is a prime number")
else:
print(num-1, " is not a prime number")
else:
print(num - 1, " is not a prime number")
print("This took ", round((time.time() - start), 2), "Seconds to complete")
prime(269996535)
code that fried my pc
import time
import multiprocessing
global range_num
range_num = []
def part_one(x, denom):
if not denom == 0:
for i in range(1, x):
y = x/denom
if y.is_integer():
range_num.append(True)
else:
range_num.append(False)
if __name__ == '__main__':
start = time.time()
x = int(input("Enter number: "))
num = x+1
range_num = []
if num-1 == 1:
print("1 is a prime number")
for i in range(0, x):
p = multiprocessing.Process(target=part_one, args=(x, i,))
p.start()
for process in range_num:
process.join()
total = 0
for ele in range(0, len(range_num)):
total = total + range_num[ele]
if total == 2:
print(num-1, " is a prime number")
else:
print(num - 1, " is not a prime number")
question from:
https://stackoverflow.com/questions/65891227/unintentionally-fried-my-pc-figuring-our-multiprocessing-for-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…