Python将参数传递给Thread类

时间:2016-01-12 11:07:44

标签: python multithreading python-multithreading

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

2 个答案:

答案 0 :(得分:2)

我不完全确定我理解为什么要为该线程创建一个新的子类。但是如果你想将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用于在参数数量未知时将参数传递给函数。在这种情况下,作为args列表传递的值是:ccc,ddd,eee。请注意,args是一个参数列表,因此您只需使用“[]”即可访问其元素。

答案 1 :(得分:1)

您在这里要做的是将downloadAllToWorldThread引用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 参数列表:

  

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

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

当你的创建实例时,不需要用括号括起参数:

ActorProxy

DEMO:

ActorId actorId = new ActorId("YourActorId");
string applicationName = "fabric:/YourAppName";
IYourActor actor = ActorProxy.Create<IYourActor>(actorId, applicationName);
await dtoActor.DoWork(new WorkItem());