0

I am learning multithreading in python. can anyone pleae tell me why the thread does not start?

code: import threading import time import logging

class Threads_2:

def __new__(cls):
    """
    this will be invoked once the creation procedure of the object begins
    """
    instance = super(Threads_2,cls).__new__(cls)
    return instance

def __init__(self):
    """
    this will be invoked once the initialisation procedure of the object begins
    """
    #self.configLogging()
    #threadinf.Thread.__init__(self)
    #self.spawnThreads()

def spawnThreads(self):
    if __name__ == "__main__":
        thread1 = threading.Thread(target=self.backgroundTask, args=(10,))
        thread1.start()

def backgroundTask(numOfLoops):
    for i in numOfLoops:
        print(2)


obj = Threads_2()
obj.spawnThreads()

error:

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
self.run()
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
self._target(*self._args, **self._kwargs)
TypeError: backgroundTask() takes 1 positional argument but 2 were given
PS D:\python workspace>
Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Please format your code. See [How do I format code blocks?](https://meta.stackoverflow.com/q/251361) – user202729 Feb 21 '21 at 10:47
  • Does this answer your question? [TypeError: method() takes 1 positional argument but 2 were given](https://stackoverflow.com/questions/23944657/typeerror-method-takes-1-positional-argument-but-2-were-given) – user202729 Feb 21 '21 at 10:48
  • The site's "related questions" search works in this case. – user202729 Feb 21 '21 at 10:49

1 Answers1

0

The function backgroundTask is in the class Threads_2. So, it should be backgroundTask(self, numOfLoops) instead of backgroundTask(numOfLoops).

Feimi
  • 1