Python 将参数传递给线程 class
Python pass argument to Thread class
Python初学者提问。我在这里看到了很多使用 "old" 方法创建线程的示例,但没有看到太多关于如何将参数传递给线程 class 的示例。我的代码如下所示......我尝试了多种不同的方法但还没有运气。非常感谢任何帮助
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))
我不确定我是否理解您为什么要为线程创建一个新的 sub-class。但是如果你想将 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 是参数列表,因此您只需使用“[ ]”即可访问其元素。
你在这里试图做的是向你的 downloadAllToWorldThread
传递一个参数列表,引用自 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))
因此,在您的代码中,您应该执行如下操作:
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
Python初学者提问。我在这里看到了很多使用 "old" 方法创建线程的示例,但没有看到太多关于如何将参数传递给线程 class 的示例。我的代码如下所示......我尝试了多种不同的方法但还没有运气。非常感谢任何帮助
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))
我不确定我是否理解您为什么要为线程创建一个新的 sub-class。但是如果你想将 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 是参数列表,因此您只需使用“[ ]”即可访问其元素。
你在这里试图做的是向你的 downloadAllToWorldThread
传递一个参数列表,引用自 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))
因此,在您的代码中,您应该执行如下操作:
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