multiprocessing: TypeError: 'int' object is not iterable

17,340
p = Process(target=main, args=p_number)

args needs to be a tuple, but you're giving it an integer. Try:

p = Process(target=main, args=(p_number,))
Share:
17,340
silverAndroid
Author by

silverAndroid

I'm a student at the University of Ottawa in Canada studying Software Engineering. I love to program and I love a coding challenge. I'm not gonna lie, I tend to give up easily unless I have the passion to finish it, which is what programming is to me, my passion. I hope at the end of the 4 years of my degree, I will be able to apply the knowledge I have learned in and out of school to apply to my daily life, whether it be programming or socializing.

Updated on July 22, 2022

Comments

  • silverAndroid
    silverAndroid almost 2 years

    I'm using the multiprocessing module in Python 3 but for some reason, it keeps throwing TypeError: 'int' object is not iterable when I run the program. This is what I did:

    def main(i):
        global urlDepth
        global row
        global counter
        urlDepth = []
        row = 0
        counter = 0
        login(i)
        crawler(MENU_URL)
    
    
    if __name__ == '__main__':
        workers = 2
        processes = []
        for p_number in range(workers):
            p = Process(target=main, args=p_number)
            p.start()
            processes.append(p)
    
        for p in processes:
            p.join()
    

    I don't understand why this is happening, could someone help me with this?

    Not a duplicate of TypeError: 'int' object is not iterable because it is the same error, yes, but it's of a different cause, please read the question/code before trying to mark this question as a duplicate.

    • Admin
      Admin over 8 years
    • isosceleswheel
      isosceleswheel over 8 years
      Which line does the error come up for?
    • silverAndroid
      silverAndroid over 8 years
      it was occurring at p = Process(target=main, args=p_number) but @Kevin already solved it.
  • Igor Markovic
    Igor Markovic about 3 years
    Thank you this worked for me as well. I'm wondering though, is there a particular reason for using p.join() in a seperate for loop? Does it matter if I use it in the same loop as p and p.start()?
  • Kevin
    Kevin about 3 years
    @IgorMarkovic Yes, you need two loops because you want all threads to be started before you try to join any of them. If you start() and join() in the same loop, then it will start thread 1, then wait for it to totally finish, then start thread 2, wait for it to totally finish, etc. It largely defeats the purpose of using threads to begin with.