0
votes

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 ?

1
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. - Ami Tavory
Have you got a heapq.py somewhere that's hiding the Python one? - Jon Clements♦
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 ? - R. Kiran
No, there is no heapq.py anywhere else. - R. Kiran

1 Answers

2
votes

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