Home > Software design >  Python threading inside a class - Passing parameters when invoking
Python threading inside a class - Passing parameters when invoking

Time:09-10

I have a python program that has several classes that when called invoke additional threads. These threads each call a separate python script to do stuff.

I followed the recommendations suggested in the question posted on "Python threading inside a class" and created the class to call the separate threads.

Below is a sample snippet of what I have.

class SomeThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        os.system('start Python_program_to_do_stuff.py')

I then instantiate several versions of this thread like this:

Some_Thread_1 = SomeThread()
Some_Thread_2 = SomeThread()
...

I set them to True

Some_Thread_1.setDaemon(True)
Some_Thread_2.setDaemon(True)

And then when actually invoking, I call each one independently

Some_Thread_1.start()

This opens a separate window to run 'Python_program_to_do_stuff.py'. Output from this program is stored at a predetermined location hard coded inside of it.

The current python program works well, but now I want to pass a very specific parameter to the class every time that it is invoked so the output data is stored in a specific location for that call. So for example, the first time I call class 'SomeThread' (ie executing Some_Thread_1.start()), store the output at location A, second time store at location B, etc. This location is not fixed and it depends on the test that I am running.

I imagine, following the reference above, that I could do something like this:

class SomeThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self, path_for_output_data):
        self.path_for_output_data = path_for_output_data
        os.system('start Python_program_to_do_stuff.py', path_for_output_data)

and then when invoking:

Some_Thread_1.start("c:/data")

but I am getting an error: "start() takes exactly 1 argument (2 given) ".

Tips are appreciated.

{As as side note, I am exploring the use of subprocess.Popen() instead of os.system(), but that is a separate mess).

CodePudding user response:

If possible you could pass the path as you instantiate each thread:

class SomeThread(threading.Thread):
    def __init__(self, path_for_output_data):
        threading.Thread.__init__(self)
        self.path_for_output_data = path_for_output_data

    def run(self):
        os.system('start Python_program_to_do_stuff.py', self.path_for_output_data)

Some_Thread_1 = SomeThread("c:/data")

Some_Thread_1.start()

CodePudding user response:

That's because Thread.start takes only 1 argument. Read on it here: Implementation

If you would like to pass arguments to a target invocation, you may use the args or kwargs parameter.

You may also modify SomeThread to take your path as an argument, that you can then call your file with:

class SomeThread(threading.Thread):
    def __init__(self, path_for_output_data):
        threading.Thread.__init__(self)
        self.path_for_output_data = path_for_output_data

    def run(self):
        # cannot do os.system(x, y) as it only takes 1 argument
        os.system('Python_program_to_do_stuff.py ' self.path_for_output_data)

# pass the path to it, and when run is called that path should be passed as a command-line argument to your file
SomeThread("c:/data").start()

  • Related