11

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.

Community
  • 1
  • 1
silverAndroid
  • 960
  • 3
  • 11
  • 29

1 Answers1

28
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,))
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • 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()? – Igor Markovic Apr 15 '21 at 09:28
  • @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. – Kevin Apr 15 '21 at 13:31