Python 3.3多线程参数序列
我一直在编写一小段代码,在FOR循环运行时测试线程,并在不同的类中运行不同的函数 该代码运行得非常好,只是如果希望它的线程时间超过10秒,IDLE会出现以下错误:Python 3.3多线程参数序列,python,multithreading,arguments,sequence,Python,Multithreading,Arguments,Sequence,我一直在编写一小段代码,在FOR循环运行时测试线程,并在不同的类中运行不同的函数 该代码运行得非常好,只是如果希望它的线程时间超过10秒,IDLE会出现以下错误: Exception in thread Thread-1: Traceback (most recent call last): File "C:\Python33\lib\threading.py", line 637, in _bootstrap_inner self.run() File "C:\Python33\lib\thr
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python33\lib\threading.py", line 637, in _bootstrap_inner
self.run()
File "C:\Python33\lib\threading.py", line 594, in run
self._target(*self._args, **self._kwargs)
TypeError: main() takes 2 positional arguments but 3 were given
threading.py模块似乎有问题,但肯定有解决方案
以下是我的代码:
import threading, time
class Application:
def __init__(self):
self.main()
def main(self)
global count
count = int(input("How many seconds do you want it to Thread?\n"))
if count:
thread = Threading(count)
else:
pass
def test(self):
print("Printing in another class.", self)
class Threading:
def __init__(self, y):
print("Starting Thread...")
number = str(count)
thread = threading.Thread(target=self.main, args=number)
thread.start()
for i in range(count):
print("Looping...", i)
time.sleep(1)
def main(self, argument):
while (int(argument)>0):
print("Threading...", argument)
argument = int(argument) - 1
Application.test(argument)
time.sleep(1)
if __name__ == '__main__':
app = Application()
如果我更改代码中的str
和int
片段,线程将根本不会运行,因为agment必须是一个序列。
所以我的问题是,我如何使代码能够运行超过10秒。我自己解决了!
参数必须是元组,因此我将原来的多线程行更改为:
thread = threading.Thread(target=self.main, args=(number,))
之前它只是args=number