I have this simple first multiprocessing code in pyton,but I encounter a BSOD when running it.Can anyone please point out what is wrong with it ?

import multiprocessing
def cracker():
    print "Hello"
    return

procs = []
for i in range(8):
    p = multiprocessing.Process(target = cracker)
    procs.append(p)
    p.start()

for p in procs:
    p.join()

Dani AI

Generated

Short, practical summary and next steps.

The hang/BSOD you saw is a symptom of unguarded subprocess creation on Windows plus how you launched the script. On Windows multiprocessing uses the "spawn" start method: each child starts a fresh Python interpreter that imports your main module. If top‑level code creates new Processes on import you get runaway spawning and fast resource exhaustion (which will hang the machine). is correct to point out protecting the spawn point, and is also right that IDLE won’t show child output reliably — run the script from a real terminal.

Concrete checklist

  • Protect the entry point (the pattern shown by ) so child processes do not re‑execute the process-creation code.
  • Run from cmd.exe / PowerShell: python -u script.py (the -u gives unbuffered stdout so child prints appear promptly). Avoid running multiprocessing tests inside IDLE.
  • Start small: test with 1 or 2 workers first, watch Task Manager for CPU/handle/memory growth, then scale up.
  • If you need to collect stdout from workers reliably, log to a file or use a multiprocessing-safe queue rather than relying on printed output.

Example approach (different from the original posts): use a Pool to split the prime-check workload and keep the process-management simple.

from multiprocessing import Pool
import math

def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(math.sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

if __name__ == '__main__':
    nums = range(2, 100000)
    with Pool(8) as p:
        results = p.map(is_prime, nums)
    print("Primes found:", sum(results))

Safety notes if you actually hit BSODs
User‑space Python rarely causes kernel crashes; a true BSOD usually indicates a bad driver, faulty RAM, or antivirus/kernel hooks. If BSODs recur, check Event Viewer, update drivers (network/graphics/AV), run a memory test, and try the script on another machine or a Linux live USB.

Recommended Answers

All 6 Replies

Upgrade to linux !

Not a viable option :P
I actually meant to do some sort of benchmarking by finding out primes in parallel by dividing the task by ranges between the 8 cores on my i7.But running the above code simply freezes the system,not to mention various BSODs at times.

Your main code is not protected for importing, which multiprocessing module uses, this works:

import multiprocessing
def cracker():
    print "Hello"
    return

if __name__ == '__main__':
    procs = []
    for i in range(8):
        p = multiprocessing.Process(target = cracker)
        procs.append(p)
        p.start()

    for p in procs:
        p.join()
commented: excellent +13

Thanks pyTony.The code runs without freezing up the system but does not produce any output

Do I have to import the module and use it in the IDLE to get that to work ?

You need to run it properly from command line double click etc. IDLE does not work.

Oh!! Thanks a lot.That saved a lot of time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.