Jun-19-2018, 08:01 AM
I have started working with threading and now I get an error on the function main(). I have made a very simplistic version of my code that reproduces this error. I have now tried a lot to fix this problem, however, I did not even understand it properly. The error can be removed (but so is the use of the code) with the "self, enable=''. I think however the arrangement of the 'a' and 'b' might cause it, but i can't solve it. What could fix this and what causes it?
Error:
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)
Error:
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!'