Python threading error - must be an iterable, not int [duplicate]
Asked Answered
S

2

27

I'm trying to calculate rolling r-squared of regression among first column and other columns in a dataframe (first column and second, first column and third etc.) But when I try threading, it kept telling me the error that

TypeError: ParallelRegression() argument after * must be an iterable, not int".

I'm wondering how do I fix this? Thanks very much!

import threading

totalThreads=3 #three different colors
def ParallelRegression(threadnum):
    for i in range(threadnum):
        res[:,i]=sm.OLS(df.iloc[:,0], df.iloc[:,i+1]).fit().rsquared
threads=[]
for threadnum in range(totalThreads):
    t=threading.Thread(target=ParallelRegression,args=(threadnum))
    threads.append(t)
    t.start()
for threadnum in range(totalThreads):
    threads[threadnum].join()

See a summary of the data (df) in the picture linked below:

enter image description here

Spam answered 20/4, 2018 at 18:35 Comment(1)
args=(threadnum) looks like it might be a typo. Is args supposed to be a tuple? One-element tuples need a trailing comma: args=(threadnum,)Kareykari
O
46

threading.Thread class needs an iterable of arguments as the args parameter. You're passing args=(threadnum) which is a single int object, you need to pass some iterable object that would allow multiple args, even when you only want to pass one arg.

args=[threadnum] would work, because that makes a list which is iterable.

Oscitancy answered 20/4, 2018 at 18:39 Comment(5)
Why answer something that could be considered a typo, and has been asked and answered countless times already? This duplicate was literally my first google result and took a whopping 5 seconds to find.Placket
Changing args to a list works on my machine, and I'd expect it to work on most Python distributions/environments, but I think it's an implementation detail that shouldn't be depended on. The docs for Thread only say that args is "the argument tuple for the target invocation". If you give it anything other than a tuple, let the buyer beware.Kareykari
@Placket why not? Is it frowned upon to answer questiosn?Oscitancy
It is frowned upon to answer obvious duplicates, yes. If you need to know why: Because it's better if we have all the answers in a single place.Placket
Please see How should duplicate questions be handled?Placket
F
11

There is another solution: add a comma at the end of "threadnum"

t=threading.Thread(target=ParallelRegression,args=(threadnum,))
Foggy answered 26/7, 2021 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.