0

I'm using the multiprocessing package in order parallelize a program. Now I have an issue on this tutorial with the following code:

def howmany_within_range(row, minimum, maximum):
  """Returns how many numbers lie within `maximum` and `minimum` in a given `row`"""
  count = 0
  for n in row:
    if minimum <= n <= maximum:
        count = count + 1
  return count    

np.random.RandomState(100)
arr = np.random.randint(0, 10, size=[200000, 5])
data = arr.tolist()

pool = mp.Pool(5)


results = [pool.apply(howmany_within_range, args=(row, 4, 8)) for row in data]


pool.close()    

print(results[:10])

I try to run with Anaconda but the program stuck then I try to run on Qt console and I get this error: module 'main' has no attribute 'spec' There is a way to run this program?

Carl
  • 53
  • 1
  • 7
  • 2
    If you're on windows you at least need to put all your code in a `main` function, otherwise each subprocess will re-import the module and create a new `Pool`. – Peter Wood Jan 23 '20 at 09:33
  • 1
    Does this answer your question? [Python Multiprocessing error: AttributeError: module '\_\_main\_\_' has no attribute '\_\_spec\_\_'](https://stackoverflow.com/questions/45720153/python-multiprocessing-error-attributeerror-module-main-has-no-attribute) – Peter Wood Jan 23 '20 at 09:36
  • I add to my code: if __name__ == '__main__': but continue to stuck. I do not succed to find how to change the anaconda settings to run in a external system terminal, that seems the solution of that question. – Carl Jan 23 '20 at 09:50
  • I solved switching to pycharm, but the computation with Pool is longer than the sequential one, this is strange – Carl Jan 23 '20 at 10:46

0 Answers0