0

I getting the error first arg must be callable on the following class

class Main():
    self.o = 0

    def sell(self, do):
        do something

    def run(self):
        try:
            _thread.start_new_thread(self.sell(do), ())
        except Exception as e:
            print(e)

if __name__ == '__main__':
    main = Main()
    main.run()


Could some advise me on how to resolve this, thanks in advance

  • It's not recommended to use the low-level `_thread` methods, but rather the higher level interface. There's some examples on implementing threading [here](https://stackoverflow.com/questions/2846653/how-can-i-use-threading-in-python) – sj95126 Oct 19 '21 at 18:48

1 Answers1

2

try this

class Main():
    self.o = 0

    def sell(self, do):
        do something

    def run(self, do):
        try:
            _thread.start_new_thread(self.sell, (do,))
        except Exception as e:
            print(e)

if __name__ == '__main__':
    do = "some-argument" 
    main = Main()
    main.run(do)

the first argument must be a callable (you did self.sell(do), wich invokes the method. the second argument you will pass the callable arguments (note the , in (do,), making this a tuple)

Also, you need some way to pass the do argument

  • thank you for answer, I readjusted the code to your sugestion, but now thread will not start, do you see any other option here – Esteban G. Gutierrez Oct 19 '21 at 16:32
  • @EstebanG.Gutierrez, that sounds like a new question. You should post it as such, and include any error messages or other output that you interpret as meaning that the thread did not start. – Solomon Slow Oct 19 '21 at 18:52