简体   繁体   中英

Python pass argument to Thread class

Python beginners question. I have seen many examples here with "old" way to create threads but not so many examples of how to pass arguments to a thread class. My code is like below... I have tried a number of different ways but no luck yet. Any help much appreciated

class downloadToWorldThread (threading.Thread):
    def __init__(self, threadID, name, counter,args=(arg1,arg2,arg3)): 
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.args.arg1 = arg1
        self.args.arg2 = arg2

        ## or this?
        #self.args.arg1 = args.arg1
        #self.args.arg2 = arg2.arg2

    def run(self):
        ##how do i access individual args?
        print "Starting " + self.name
        print "arg is " +  self.args.arg2

        downloadToMyHouse(self.args.arg1,self.args.arg2,self.args.arg3)
        print "Exiting " + self.name



def downloadAllToWorld(aaa,bbb,ccc,ddd,eee,fff):

    # Create new threads
    ##thread
    thread1 = downloadToWorldThread(1, "blah1-1", 1,args=(arg1,arg2,arg3))
    ##thread2
    thread2 = downloadToWorldThread(2, "blah2-2", 2, args=(arg1,arg2,arg3))

I am not entirely sure I understand why you are creating a new sub-class for the thread. But if you want to pass the args to the subclass you should do something:

class downloadAllToWorldThread(threading.Thread):
    def __init__(self, threadID, name, counter, *args):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.args = args
    def run(self):
        print('Args are: {}'.format(self.args))
        downloadToMyHouse(self.args[0],self.args[1],self.args[2])

def downloadAllToWorld(aaa,bbb,ccc,ddd,eee):
    thread1 = downloadAllToWorldThread(1,"blah1-1", 1, ccc, ddd, eee)

args is used to pass arguments to a function when the number of argument is not known. In this case the values passed as args list are: ccc, ddd, eee . Please do note that args is a list of arguments so you can access its element just by using "[ ]".

What you are trying to do here, is to pass to your downloadAllToWorldThread a list of arguments, quoting from Python docs :

4.7.3. Arbitrary Argument Lists Finally, the least frequently used option is to specify that a function can be called with an arbitrary number of arguments. These arguments will be wrapped up in a tuple (see Tuples and Sequences). Before the variable number of arguments, zero or more normal arguments may occur.

 def write_multiple_items(file, separator, *args): file.write(separator.join(args)) 

So, in your code, you should be doing something like:

class downloadToWorldThread (threading.Thread):
    def __init__(self, threadID, name, counter,*args): 
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.args = args

Then pass self.args as it is to downloadToMyHouse method.

def run(self):
        print('Args are: {}'.format(self.args))
        downloadToMyHouse(self.args)

Finally, in downloadToMyHouse method, upack the self.args , example:

def downloadToMyHouse(self, *args):

    for i in args:
        print i
    #OR
    print args[0] #access specific element of args through indexing

And when your create instances, no need to enclose arguments with brackets:

thread1 = downloadToWorldThread(1, "blah1-1", 1, arg1, arg2, arg3)

DEMO:

class myThread(threading.Thread):
    def __init__(self, threadID, name, counter, *args):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.counter = counter
        self.args = args
    def run(self):
        print 'Starting Thread {0} named {1}, counter {2}'.format(self.threadID, self.name, self.counter)
        for i in self.args:
            print i


>>> t1 = myThread(1, 'Thread1', 2, 'ONE','TWO','THREE')
>>> t1.start()
Starting Thread 1 named Thread1, counter 2
>>> 
ONE
TWO
THREE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM