Search code examples
python-3.xmultithreadingooppython-multithreading

TypeError: __init__() takes from 1 to 6 positional arguments but 7 were given


I am using multithreading concept in my script. But I am getting the above error while running the script. I used the same code in python 2.7 and it worked fine but in python 3.6 it is giving me error. Could you please help me how to solve this?

Code Which I am using-

class ThreadWithReturnValue(Thread):
    def __init__(self, group=None, target=None, name=None,
                 args=(), kwargs={}, Verbose=None):
        Thread.__init__(self, group, target, name, args, kwargs, Verbose)
        self._return = None
    def run(self):
        if self._Thread__target is not None:
            self._return = self._Thread__target(*self._Thread__args,
                                                **self._Thread__kwargs)
    def join(self):
        Thread.join(self)
        return self._return

May be syntax is different in python 3.6 Please help me to solve this error.

Error:

Traceback (most recent call last):
  File "/app/HTA/Scripts/Python/HTA_Consump_Layer_Procs.py", line 588, in <module>
    main()
  File "/app/HTA/Scripts/Python/HTA_Consump_Layer_Procs.py", line 585, in main
    parallel_Execution()
  File "/app/HTA/Scripts/Python/HTA_Consump_Layer_Procs.py", line 395, in parallel_Execution
    p2 = ThreadWithReturnValue(target = partial(parallel_temp2, unique_exec_seq, df ))
  File "/app/HTA/Scripts/Python/HTA_Consump_Layer_Procs.py", line 365, in __init__
    Thread.__init__(self, group, target, name, args, kwargs, Verbose)
TypeError: __init__() takes from 1 to 6 positional arguments but 7 were given

Solution

  • Well, I did a trial of ignoring Verbose in Thread.__init__() Then it works fine to my end. Find my demo code and output. Prior Python<3 used Verbose in Thread but after 3.x+ Verbose is ignored.

    from threading import Thread
    
    def foo(bar):
        print('hello {0}'.format(bar))
        return "foo"
    
    class ThreadWithReturnValue(Thread):
        def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None):
            Thread.__init__(self, group, target, name, args, kwargs)
            self._return = None
    
        def run(self):
            if self._target is not None:
                self._return = self._target(*self._args, **self._kwargs)
    
        def join(self, *args):
            Thread.join(self, *args)
            return self._return
    
    
    twrv = ThreadWithReturnValue(target=foo, args=('world!',))
    twrv.start()
    print(twrv.join())
    
    # Output
    # hello world!
    # foo