0

I am trying some simple programs which involve multiprocessing features in Python.

The code is given below:

from multiprocessing import Process, Queue

def print_square(i):
  print i*i

if __name__ == '__main__':
  output = Queue()
  processes = [Process(target=print_square,args=(i,)) for i in range(5)]

  for p in processes:
    p.start()

  for p in processes:
    p.join()

However, this gives an error message AttributeError: 'module' object has no attribute 'heappush'. The complete output upon executing the script is given below:

Traceback (most recent call last):
  File "parallel_3.py", line 15, in 
    output = Queue()
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\multi
processing\__init__.py", line 217, in Queue
    from multiprocessing.queues import Queue
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\multi
processing\queues.py", line 45, in 
    from Queue import Empty, Full
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\Queue
.py", line 212, in 
    class PriorityQueue(Queue):
  File "C:\Users\abc\AppData\Local\Continuum\Anaconda2\lib\Queue
.py", line 224, in PriorityQueue
    def _put(self, item, heappush=heapq.heappush):
AttributeError: 'module' object has no attribute 'heappush'

The code compiles fine if the output=Queue() statement is commented. What could be possibly causing this error ?

4
  • This works fine by me. It looks like something is wrong with your environment. If you don't get better answers, perhaps you should consider reinstalling your python distribution. Commented Sep 22, 2016 at 12:15
  • 4
    Have you got a heapq.py somewhere that's hiding the Python one? Commented Sep 22, 2016 at 12:17
  • I tried running it on my university machine, which works just fine. As you said, seems to be an environment issue. But I installed python in both systems, and can't think of any difference between the two, which might be causing the issue. Any pointers on where I should start looking at ? Commented Sep 22, 2016 at 12:25
  • 1
    No, there is no heapq.py anywhere else. Commented Sep 22, 2016 at 12:26

1 Answer 1

2

Your filename should be the package name. Change to another filename such as heap and it will work.

Sign up to request clarification or add additional context in comments.

Comments

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.