简体   繁体   English

python 多处理未启动

[英]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我想使用 2 个单独的 CPU 内核、我的脚本从一个大列表中打印,它运行的证据如下

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该过程需要使用.start()方法启动

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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