Python threading daemon error when extending Thread class
Question:
I’ve been trying to extend my Thread class to return something from a target function but I want to keep the daemon = True. The default daemon is False so I need to pass the True value into the Thread.__init__()
and when I try: __init__() takes from 1 to 6 positional arguments but 7 were given
Here’s the code:
class Thread1(Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs={}, daemon = None):
Thread.__init__(self, group, target, name, args, kwargs, daemon)
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
I’ve tried to remove the self parameter but it doesn’t work then.
Answers:
The Thread.__init__()
method takes up to 6 arguments, but you are passing 7 arguments to it, including daemon. This is causing the error message you’re seeing.
To fix this issue, you can remove the daemon parameter from the Thread.__init__()
method call and instead set the daemon attribute of the thread object after it has been created.
Here’s an updated version of your code:
class Thread1(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
super().__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self._return = None
self.daemon = True
def run(self):
if self._target is not None:
self._return = self._target(*self._args, **self._kwargs)
def join(self, *args):
super().join(*args)
return self._return
I’ve been trying to extend my Thread class to return something from a target function but I want to keep the daemon = True. The default daemon is False so I need to pass the True value into the Thread.__init__()
and when I try: __init__() takes from 1 to 6 positional arguments but 7 were given
Here’s the code:
class Thread1(Thread):
def __init__(self, group=None, target=None, name=None,
args=(), kwargs={}, daemon = None):
Thread.__init__(self, group, target, name, args, kwargs, daemon)
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
I’ve tried to remove the self parameter but it doesn’t work then.
The Thread.__init__()
method takes up to 6 arguments, but you are passing 7 arguments to it, including daemon. This is causing the error message you’re seeing.
To fix this issue, you can remove the daemon parameter from the Thread.__init__()
method call and instead set the daemon attribute of the thread object after it has been created.
Here’s an updated version of your code:
class Thread1(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}):
super().__init__(group=group, target=target, name=name, args=args, kwargs=kwargs)
self._return = None
self.daemon = True
def run(self):
if self._target is not None:
self._return = self._target(*self._args, **self._kwargs)
def join(self, *args):
super().join(*args)
return self._return