5

I have this method in my class called "interaction":

class Interaction:

    def PreparaThreadBrowser(self, User, Password):
       t = ThreadBrowser(args=(User, Password), )
       t.start()

that ivokes a thread with User and Pass as parameters.

Then I have the class called "ThreadBrowser" with this "run" method:

class ThreadBrowser(threading.Thread):

    def run (self, user, password):

        self.User = user
        self.Pass = password
        print(self.User, self.Pass)

but it turns out that I'm certainly struggling sending the arguments to the thread function because it gives an execution error:

TypeError: run() missing 2 required positional arguments: 'user' and 'password'

Any idea how to solve this ?

2
  • Could you fix your indentation and add the code where you actually call ThreadBrowser.run()? Commented Jul 8, 2018 at 21:13
  • @Primusa Thread.run is automatically invoked by Thread.start. Commented Jul 8, 2018 at 21:19

2 Answers 2

2

The args constructor argument can't provide additional arguments to run. It provides the arguments to target, which you aren't specifying. Specifically:

  • the default implementation of run() invokes self.target(*self._args, **self._kwargs), where _args and _kwargs are provided by the constructor, and default to empty tuple and dict respectively.

  • by implementing run you opted out of Python invoking target(*args) automatically. It is then up to your code to store the state that your run will pick up. The idiomatic option is to store it in the constructor, much like your run method does now.

For example:

class ThreadBrowser(threading.Thread):
    def __init__(self, user, password):
        super(ThreadBrowser, self).__init__()
        self.User = user
        self.Pass = password

    def run(self):
        print(self.User, self.Pass)

The ThreadBrowser can then be constructed naturally as ThreadBrowser(username, password) and started with t.start() as before.

Sign up to request clarification or add additional context in comments.

3 Comments

That makes sense however it's giving a compiling error: imgur.com/a/F0SPLME
@GonçaloBaptista The error appears like an unrelated issue of mixing tabs and spaces. The code in the answer consistently uses spaces; did you copy/paste it?
yes, didn't know couldn't copy paste. Sorry. It worked :D Thank you so much
0

Run takes 2 arguments user, pass

import threading



class Interaction:
    def PreparaThreadBrowser(self, User, Password):
        t = ThreadBrowser(args=(User, Pass), )
        t.start()



class ThreadBrowser(threading.Thread):

    def run (self, user, password):
        self.User = user
        self.Pass = password
        print(self.User, self.Pass)



t = ThreadBrowser()
t.run("user", "pass")

2 Comments

If i use like you told me too, the paramaters arrive correctly in the run method but the thread isn't invoked !
Invoking run directly is wrong, the thread must always be invoked using the start() method.

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.