Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

multithreading - Python threading error - must be an iterable, not int

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...