我遇到Python线程问题并在参数中发送字符串.
def processLine(line) :
print "hello";
return;
Run Code Online (Sandbox Code Playgroud)
.
dRecieved = connFile.readline();
processThread = threading.Thread(target=processLine, args=(dRecieved));
processThread.start();
Run Code Online (Sandbox Code Playgroud)
其中dRecieved是连接读取的一行字符串.它调用一个简单的函数,到目前为止只有一个打印"hello"的工作.
但是我收到以下错误
Traceback (most recent call last):
File "C:\Python25\lib\threading.py", line 486, in __bootstrap_inner
self.run()
File "C:\Python25\lib\threading.py", line 446, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: processLine() takes exactly 1 arguments (232 given)
Run Code Online (Sandbox Code Playgroud)
232是我试图传递的字符串的长度,所以我猜它会将其分解为每个字符并尝试传递这样的参数.如果我只是正常调用函数,它工作正常,但我真的想将它设置为一个单独的线程.
我想在线程中运行一些计时器?但它显示错误TypeError: start() argument after * must be an iterable, not int?我该如何解决?
while 1:
try:
_thread.start_new_thread(self.timer0.start,100)
_thread.start_new_thread(self.timer1.start,150)
_thread.start_new_thread(self.timer2.start,200)
_thread.start_new_thread(self.timer3.start,250)
_thread.start_new_thread(self.timer4.start,300)
break
except:
print ("Error: unable to start thread")
break
Run Code Online (Sandbox Code Playgroud)