简体   繁体   中英

python multiprocessing doesn't start

I want to print from a big list using 2 separate CPU cores, my script, and evidence of it running is below is below

It finishes instantly, I don't think the 2 processes work

import multiprocessing as mp
import numpy as np

A = np.linspace(0,99999999,999999)

def print_stuff(i):
    for j in i:    # i want two seperate proccessores to print half of this list each 
        print(A[j]) 

def do_stuff():
    print("doing stuff")
    kk = range(int(len(A/2)))
    kk2 = []
    for i in kk:
        kk2.append(i+kk[-1])
    print(len(kk2))
    p1 = mp.Process(target = print_stuff, args = kk)
    p2 = mp.Process(target = print_stuff, args = kk2)
    print("done stuff")

if __name__ == "__main__":
    do_stuff()

The process need to be started with .start() method

import multiprocessing as mp
import numpy as np

A = np.linspace(0,99999999,999999)

def print_stuff(i):
    for j in i:    # i want two seperate proccessores to print half of this list each 
        print(A[j]) 

def do_stuff():
    print("doing stuff")
    kk = range(int(len(A/2)))
    kk2 = []
    for i in kk:
        kk2.append(i+kk[-1])
    print(len(kk2))
    p1 = mp.Process(target = print_stuff, args = kk)
    p2 = mp.Process(target = print_stuff, args = kk2)

    # Starting threads
    p1.start()
    p2.start()

    # This will terminate the thread when they done their job(s)
    p1.join()
    p2.join()

    print("done stuff")

if __name__ == "__main__":
    do_stuff()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM