title: Parallel and conditional: NoneType object has no attribute '__dict__'
body: <p>For more setup, see <a href="https://stackoverflow.com/questions/30741061/parallel-python-iteration/30745634?noredirect=1#comment49589964_30745634">this question</a>. I want to create lots of instances of class <code>Toy</code>, in parallel. Then I want to write them to an xml tree.</p>
  
  <pre><code>import itertools
  import pandas as pd
  import lxml.etree as et
  import numpy as np
  import sys
  import multiprocessing as mp
  
  
  def make_toys(df):
      l = []
      for index, row in df.iterrows():
          toys = [Toy(row) for _ in range(row['number'])]
          l += [x for x in toys if x is not None]
      return l
  
  
  class Toy(object):
      def __new__(cls, *args, **kwargs):
          if np.random.uniform() &lt;= 1:
              return super(Toy, cls).__new__(cls, *args, **kwargs)
  
      def __init__(self, row):
          self.id = None
          self.type = row['type']
  
      def set_id(self, x):
          self.id = x
  
      def write(self, tree):
          et.SubElement(tree, "toy", attrib={'id': str(self.id), 'type': self.type})
  
  
  if __name__ == "__main__":
      table = pd.DataFrame({
          'type': ['a', 'b', 'c', 'd'],
          'number': [5, 4, 3, 10]})
  
      n_cores = 2
      split_df = np.array_split(table, n_cores)
  
      p = mp.Pool(n_cores)
      pool_results = p.map(make_toys, split_df)
      p.close()
      p.join()
      l = [a for L in pool_results for a in L]
  
      box = et.Element("box")
      box_file = et.ElementTree(box)
  
      for i, toy in itertools.izip(range(len(l)), l):
          Toy.set_id(toy, i)
  
      [Toy.write(x, box) for x in l]
  
      box_file.write(sys.stdout, pretty_print=True)
  </code></pre>
  
  <p>This code runs beautifully. But I redefined the <code>__new__</code> method to only have a random chance of instantiating a class. So if I set <code>if np.random.uniform() &lt; 0.5</code>, I want to create half as many instances as I asked for, randomly determined. Doing this returns the following error:</p>
  
  <pre><code>Exception in thread Thread-3:
  Traceback (most recent call last):
    File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
      self.run()
    File "/usr/lib/python2.7/threading.py", line 763, in run
      self.__target(*self.__args, **self.__kwargs)
    File "/usr/lib/python2.7/multiprocessing/pool.py", line 380, in _handle_results
      task = get()
  AttributeError: 'NoneType' object has no attribute '__dict__'
  </code></pre>
  
  <p>I don't know what this even means, or how to avoid it. If I do this process monolithically, as in <code>l = make_toys(table)</code>, it runs well for any random chance.</p>
  
  <h3>Another solution</h3>
  
  <p>By the way, I know that this can be solved by leaving the <code>__new__</code> method alone and instead rewriting <code>make_toys()</code> as</p>
  
  <pre><code>def make_toys(df):
      l = []
      for index, row in df.iterrows():
          prob = np.random.binomial(row['number'], 0.1)
          toys = [Toy(row) for _ in range(prob)]
          l += [x for x in toys if x is not None]
      return l
  </code></pre>
  
  <p>But I'm trying to learn about the error.</p>