многопроцессорный пул зависает в ноутбуке Jupyter - PullRequest
2 голосов
/ 26 июня 2019

У меня есть очень простой скрипт, который выглядит следующим образом:

import multiprocessing as multi

def call_other_thing_with_multi():
    P = multi.Pool(3)
    P.map(other_thing, range(0,5))
    P.join()


def other_thing(arg):
    print(arg)
    return arg**2.

call_other_thing_with_multi()

Когда я это называю, мой код висит вечно. Это на Windows с Python 2.7.

Спасибо за любые советы!

1 Ответ

1 голос
/ 26 июня 2019

Согласно документации , вам необходимо позвонить close() до join():

import multiprocessing as multi

def call_other_thing_with_multi():
    P = multi.Pool(3)
    P.map(other_thing, range(0,5))
    P.close() # <-- calling close before P.join()
    P.join()
    print('END')

def other_thing(arg):
    print(arg)
    return arg**2.

call_other_thing_with_multi()

Отпечатки:

0
1
2
3
4
END

РЕДАКТИРОВАТЬ: лучше использоватьменеджер контекста, чтобы не забыть позвонить close():

def call_other_thing_with_multi():
    with multi.Pool(3) as P:
        P.map(other_thing, range(0,5))
    print('END')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...