0

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 obviously occurs because of the "self, enable=''. What could fix this and what causes it?

PS: There are posts with an error similar to this, but they all refer to super() being used inside a function.

The error is:

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)

My code:

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!'
Zen_Master
  • 21
  • 7
  • Is the indentation correct, if so you are currently defining a and b inside of the class, in the local scope of the class during instantiation, but you never create an instance of it in the example code. I have never seen code using a class like this but interestingly this works from a syntax perspective. Your current example code throws `Traceback (most recent call last): File "test.py", line 33, in a.start() NameError: name 'a' is not defined` as-is. – d parolin Jun 18 '18 at 16:38
  • Possible duplicate of [Run Class methods in threads (python)](https://stackoverflow.com/questions/15365406/run-class-methods-in-threads-python) – Tsyvarev Jun 18 '18 at 16:41
  • Thanks for the hint. Bad codeformating by me, fixed the indentation. Should display the inital error now. – Zen_Master Jun 18 '18 at 16:49

0 Answers0