Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
70 views
in Technique[技术] by (71.8m points)

multithreading - unintentionally fried my pc.. figuring our multiprocessing for python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

...my programs started to crash one by one and then finally bluescreen. Can someone explain why this happened...?

Your PC has inadequate cooling. CMOS logic generates more heat the harder it works. It will most likely get better after you let it cool down.

Yours isn't the only PC that overheats. I've had to deal with overheated PCs on and off throughout my entire career. Long computations will shut down the laptop that I use for work right now unless I set it up on blocks and I set up a small fan on my desk to blow air over and under it.

If it's in a desktop case or a tower case, you can try taking the covers off and pointing a fan at it. If it's a rackmount server, then either the room you are running it in doesn't have enough cooling, or else you should ask for your money back.


P.S., A small desk fan is essential if you ever need to connect a naked, internal hard drive to your PC. They are not designed to work without forced air flow.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...