我已经开始使用线程,现在我在函数main()上遇到错误。我已经制作了一个非常简单的代码版本来重现这个错误。我现在已经尝试了很多来解决这个问题,但是,我甚至都不理解它。错误显然是因为“self,enable =''。什么可以解决这个问题以及导致它的原因?
PS:有些帖子的错误类似于此,但它们都是指函数内部使用的super()。
错误是:
Exception in thread main:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
TypeError: main() takes at least 1 argument (0 given)
我的代码:
import threading
from time import sleep
number = 0
alive = True
class Server(object):
def main(self, enable=''):
self.enable = int(enable)
#here we have some loops that rely on the variables like "enable"
def background():
global number
while alive:
if number < 20:
number=number+1
print 'Number: {0}'.format(number)
sleep(1)
else:
print "20 seconds are over!"
break
a = threading.Thread(name='main', target=main)
b = threading.Thread(name='background', target=background)
a.start()
b.start()
while alive: #this is just to terminate early
try:
sleep(.1)
except KeyboardInterrupt:
alive = False
print 'Terminated!'