1

I am trying to implement multi threading with oops


class test:
    def printer(self):
        for ctr in range(1000000):
            print("hello")

    def printHi(self):
        for ctr in range(1000000):
            print("hi")
            
if __name__ == "__main__":
    test1 = test()

    t1 = threading.Thread(target=test1.printHi, args=(10,))
    t2 = threading.Thread(target=test1.printer, args=(10,))
    t1.start()
    t2.start()
    print("Done!")

But the test1.printHi is expecting me to pass self

Exception in thread Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 973, in _bootstrap_inner
Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.9/threading.py", line 973, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.9/threading.py", line 910, in run
    self.run()
  File "/usr/lib/python3.9/threading.py", line 910, in run
    self._target(*self._args, **self._kwargs)    self._target(*self._args, **self._kwargs)

TypeError: printHi() takes 1 positional argument but 2 were givenTypeError: 
printer() takes 1 positional argument but 2 were given
Done!

After passing self it is not being multi threaded any more It

t1 = threading.Thread(target=test1.printHi())
t2 = threading.Thread(target=test1.printer())
t1.start()
print("next")
t2.start()

Its first printing all hi and then hello at last next is getting printed but when I implement them like functions its working properly they are getting printed at once combined. Whats the right way to implement it properly such that both threads runs simultaneously...

4
  • You cannot have two loops working simultaneously in python, the GIL will only allow one thread to execute code, the other will have to wait. Commented Dec 25, 2021 at 7:06
  • If you want to execute both loops at the same time then you should use multiprocessing, which has limitations in terms of memory. Commented Dec 25, 2021 at 7:07
  • As pointed out by jiri, you must use sleep so the GIL will be transferred if you aim to use threading. Commented Dec 25, 2021 at 7:09
  • 2
    Threading in python only improves performance if your task is waiting on external resources (like reading from disk or internet) where python threads will release the GIL while waiting, so other threads can run. Commented Dec 25, 2021 at 7:31

1 Answer 1

2

You seem to be passing an extra 10 to the methods; try:


class test:
    def printer(self):
        for ctr in range(10):
            print("hello")
            time.sleep(1)

    def printHi(self):
        for ctr in range(10):
            print("hi")
            time.sleep(1)
            
if __name__ == "__main__":
    test1 = test()

    t1 = threading.Thread(target=test1.printHi, args=())
    t2 = threading.Thread(target=test1.printer, args=())
    t1.start()
    t2.start()
    print("Done!")

Or, if you want to keep the parameter, the functions need to accept it:


class test:
    def printer(self, n):
        for ctr in range(10):
            print("hello", n)
            time.sleep(1)

    def printHi(self, n):
        for ctr in range(10):
            print("hi", n)
            time.sleep(1)
            
if __name__ == "__main__":
    test1 = test()

    t1 = threading.Thread(target=test1.printHi, args=(10,))
    t2 = threading.Thread(target=test1.printer, args=(10,))
    t1.start()
    t2.start()
    print("Done!")
Sign up to request clarification or add additional context in comments.

8 Comments

What's the new error message?
object.methodname this is giving me a error that it need self
object.methodname() is fixing the error but its misbehaving hi and hello are not getting printed at once
None of the errors you've posted mention needing self...
object.methodname will automatically supply self, so that's probably not the problem...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.