简体   繁体   中英

python multiprocessing does not run functions

I just want to see a simple code implementation of multiprocessing on windows, but it doesn't enter/run functions neither in jupyternotebook or running saved .py

import time
import multiprocessing
s=[1,4]
def subu(remo):
    s[remo-1]=remo*9
    print(f'here{remo}')
    return
if __name__=="__main__":
    p1=multiprocessing.Process(target=subu , args=[1])
    p2=multiprocessing.Process(target=subu , args=[2])
    p1.start()
    p2.start()
    p1.join()
    p2.join()
#     print("2222here")
print(s)
input()

the output by.py is:

[1, 4]
[1, 4]

and the output by jupyternotebook is:

[1,4]

which I hoped to be:

here1
here2
[9,18]

what's wrong with code above? and what about this code:

import concurrent
thread_num=2
s=[1,4]
def subu(remo):
    s[remo-1]=remo*9
    print(f'here{remo}')
    return
with concurrent.futures.ProcessPoolExecutor() as executor:
## or if __name__=="__main__":
##...    with concurrent.futures.ProcessPoolExecutor() as executor:
    results=[executor.submit(subu,i) for i in range(thread_num)]
    for f in concurrent.futures.as_completed(results):
        print(f.result())
input()

doesnot run at all in jupyter pulling error

BrokenProcessPool: A process in the process pool was terminated abruptly while the future was running or pending.

I kinda know I can't expect jupyter to run multiprocessing. but saved.py also can't run it. and it exits without waiting for input()

There are a couple of potential problems. The worker function needs to be importable (at least on Windows) so that it can be found by the subprocess. And since subprocess memory isn't visible to the parent, the results need to be returned. So, putting the worker in a separate module

subumodule.py

def subu(remo):
    remo = remo*9
    print(f'here{remo}')
    return remo

And using a process pool's existing infrastructure to return a worker return value to the parent. You could

import time
import multiprocessing
if __name__=="__main__":
    with multiprocessing.Pool(2) as pool:
        s = list(pool.map(subu, (1,2))) #here
    print(s)
    input()

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