title: Python: AttributeError: Can't pickle local object 'writeBuf.<locals>.write'
body: <p>I am not at all familiar with Python, I usually do Ruby or JS. But I need to write a benchmarking script on a system that runs Python. What I'm trying to do is create a small script that gets a file size and the number of threads and write a random buffer. This is what I got after 2 hours of fiddling:</p>
  
  <pre><code>from multiprocessing import Pool
  import os, sys
  
  def writeBuf(buf):
      def write(n):
          f = open(os.path.join(directory, 'n' + str(n)), 'w')
          try:
              f.write(buf)
              f.flush()
              os.fsync(f.fileno)
          finally:
              f.close()
      return write
  
  if __name__ == '__main__':
      targetDir = sys.argv[1]
      numThreads = int(sys.argv[2])
      numKiloBytes = int(sys.argv[3])
      numFiles = int(102400 / numKiloBytes)
  
      buf = os.urandom(numKiloBytes * 1024)
  
      directory = os.path.join(targetDir, str(numKiloBytes) + 'k')
      if not os.path.exists(directory):
          os.makedirs(directory)
  
      with Pool(processes=numThreads) as pool:
          pool.map(writeBuf(buf), range(numFiles))
  </code></pre>
  
  <p>But it throws the error: <code>AttributeError: Can't pickle local object 'writeBuf.&lt;locals&gt;.write'</code></p>
  
  <p>I have tried before to use <code>write</code> without the closure, but I got an error when I tried to define the function inside the <code>__name__ == '__main__'</code> part. Omitting that <code>if</code> also lead to an error and I read that it is required for <code>Pool</code> to work.</p>
  
  <p>What's supposed to be just a tiny script turned into a huge ordeal, can anyone point me the right way?</p>