Error using python multiprocessing module with generator function.

Can someone explain what is wrong with below code

from multiprocessing import Pool
def sq(x):
    yield x**2
p = Pool(2)

n = p.map(sq, range(10))

I get the following error

MaybeEncodingError Traceback (last call last) in () 5 p = Pool (2) 6 ----> 7 n = p.map (sq, range (10))

/home/devil/anaconda3/lib/python3.4/multiprocessing/pool.py in map (self, func, iterable, chunksize) 258 in the list that returns. 259 '' '→ 260 return self._map_async (func, iterable, mapstar, chunksize) .get () 261 262 def starmap (self, func, iterable, chunksize = None):

/home/devil/anaconda3/lib/python3.4/multiprocessing/pool.py in get (self, timeout) 606 return self._value 607: → 608 raise self._value 609 610 def _set (self, i, obj):

MaybeEncodingError: : '[,]'. : 'TypeError ( " ",)'

.

+4
1

, . : yield return, sq . Pool .

, Windows .

Attempt to start a new process before the current process
has finished its bootstrapping phase.

This probably means that you are on Windows and you have
forgotten to use the proper idiom in the main module:

if __name__ == '__main__':

, , :

, python, python .., "if main block" "

__name__=="__main__" :

from multiprocessing import Pool

def sq(x):
    return x**2

if __name__=="__main__":
    p = Pool(2)
    n = p.map(sq, range(10))
    print(n)

:

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

: , imap

n = p.imap(sq, range(10))

n -. ( ), , ,

print(list(n))

, , imap , map

+4

Source: https://habr.com/ru/post/1668810/


All Articles