1

I'm new on multiprocessing on python and I just saw a code to run a function in multiprocessing but that code gave me an error.

import multiprocessing
import os

def foo():
    return

if __name__ == "__main__" :
    for process_idx in range(multiprocessing.cpu_count()):
        p = multiprocessing.Process(target=foo)
        os.system("taskset -p -c %d %d" % (process_idx % multiprocessing.cpu_count(), os.getpid()))
        p.start()

Error:

File "multiprocessing.py", line 8, in <module>
    for process_idx in range(multiprocessing.cpu_count()):
AttributeError: module 'multiprocessing' has no attribute 'cpu_count'

I have installed the multiprocessing package, too.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • 1
    Welcome to stack overflow @Jesusmoyano, What version of python are you running? – akozi Nov 15 '18 at 16:48
  • 3
    Stop calling your file `multiprocessing.py`. – user2357112 Nov 15 '18 at 16:58
  • 2
    You shouldn't have to install the `multiprocessing` package, it's a standard library that's included with Python—therefore you shouldn't give your own script the same name... – martineau Nov 15 '18 at 16:59
  • @akozi Im using python 3.6. – Jesus moyano Nov 15 '18 at 17:13
  • @user2357112 seems that the problem disapear renaming the program. But now im on MacOs and dont have the command TaskSet... – Jesus moyano Nov 15 '18 at 17:13
  • @martineau what happens ? I just changed the name and it works :D Thanks. But now im on MacOs and dont have the command TaskSet... – Jesus moyano Nov 15 '18 at 17:14
  • Jesus: I don't know what you mean about not having "TaskSet". Anyway, what happens is your script ends-up `import`ing itself instead of the one in the standard library, and since your module/script doesn't define such a function—it causes the `AttributeError`. You almost always want to avoid using any names in your code (including the filenames of scripts) that conflict/collide with standard Python names. Besides this kind of problem (preventing you from using the built-in), it also makes it hard for others to read and understand your code. Also see the answer to the linked question. – martineau Nov 15 '18 at 17:31
  • 1
    @martineau Thanks a lot! – Jesus moyano Nov 15 '18 at 17:32
  • Jesus: You're welcome—if it makes you feel any better, it's a mistake many beginners make. – martineau Nov 15 '18 at 17:39

0 Answers0