0

我一直在编写一小段代码来测试 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\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()

如果我在代码中更改strint部分,线程将根本不会运行,因为 agument 必须是一个序列。 所以我的问题是,如何使代码能够运行超过 10“秒”。

4

1 回答 1

1

我自己想通了!参数需要是一个元组,所以我将原来的多线程行更改为以下内容:

 thread = threading.Thread(target=self.main, args=(number,))

在这之前args=number

于 2013-06-12T14:28:52.697 回答