1

I am experiencing with Python Multiprocessing and got stuck with passing a list of object as pool.map arguments. The following codes print [None, None] instead of actual names. Can anybody shed some light on it?

from multiprocessing import Pool

class Item:
  def __init__(self, firstname, lastname):
    self.firstname = firstname
    self.lastname = lastname

def get_items():
  items = []
  names = ['Joe Smith', 'Rick Harvard']

  for name in names:
      item = Item(name.split(' ')[0], name.split(' ')[1])
      items.append(item)

  return items

def f(name):
    print(name.firstname, name.lastname)
    
if __name__ == '__main__':
  p = Pool(16)
  print(p.map(f, get_items()))
  p.close()
  p.join()

3
  • "print [None, None] ": This is correct, you call def f(... two times and def f(... return two times None. Commented Oct 7, 2019 at 18:19
  • but why it didn't print 'Joe,Smith' and 'Rick,Harvard'? Commented Oct 7, 2019 at 18:25
  • Did you say: print(name.... in def f(... did not show up? Commented Oct 7, 2019 at 18:27

1 Answer 1

1

The print method returns None and you call it twice

change your f function to

def f(name):
    return name.firstname, name.lastname
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.