Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python将参数传递给线程类_Python_Multithreading_Python Multithreading - Fatal编程技术网

Python将参数传递给线程类

Python将参数传递给线程类,python,multithreading,python-multithreading,Python,Multithreading,Python Multithreading,Python初学者问题。我在这里看到了许多创建线程的“旧”方法的示例,但没有太多关于如何向线程类传递参数的示例。我的代码如下。。。我尝试了很多不同的方法,但还没有成功。非常感谢您的帮助 class downloadToWorldThread (threading.Thread): def __init__(self, threadID, name, counter,args=(arg1,arg2,arg3)): threading.Thread.__init__(sel

Python初学者问题。我在这里看到了许多创建线程的“旧”方法的示例,但没有太多关于如何向线程类传递参数的示例。我的代码如下。。。我尝试了很多不同的方法,但还没有成功。非常感谢您的帮助

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))

我不能完全理解为什么要为线程创建一个新的子类。但是,如果要将args传递给子类,则应执行以下操作:

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用于在参数数目未知时将参数传递给函数。在这种情况下,作为参数列表传递的值是:ccc、ddd、eee。请注意,args是一个参数列表,因此您只需使用“[]”即可访问其元素

您在此处尝试的是将参数列表传递给您的
downloadAllToWorldThread
,引用自:

4.7.3。任意参数列表最后,最不常用的选项是指定可以使用任意参数调用函数 参数的数量。这些参数将封装在一个元组中 (参见元组和序列)。在变量数量的参数之前, 可能出现零个或多个正常参数

def write_multiple_items(file, separator, *args):
   file.write(separator.join(args))
因此,在代码中,您应该执行以下操作:

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
然后将
self.args
原样传递给
downloadToMyHouse
方法

def run(self):
        print('Args are: {}'.format(self.args))
        downloadToMyHouse(self.args)
最后,在
downloadToMyHouse
方法中,上传
self.args
,例如:

def downloadToMyHouse(self, *args):

    for i in args:
        print i
    #OR
    print args[0] #access specific element of args through indexing
创建实例时,无需用括号括起参数:

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

演示:

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

Hi@Algold,我是这样做的,因为我遵循本教程(第二个示例),我需要轮询线程是否完成。我的最后一个问题是如何在run()函数中访问self.args中的各个元素?我已经编辑了添加run()函数以及如何访问args列表的答案。@user1843591。。这很好…只要确保你通过链接的文档来清除你头脑中的东西。