简体   繁体   English

Python如何使用未知数量的参数初始化线程?

[英]Python how to initialize thread with unknown number of arguments?

I'm having trouble using the starred expressions in combination with fixed argument lists when attempting to create threads. 在尝试创建线程时,我无法将已加星标的表达式与固定参数列表结合使用。

Consider the following code: 请考虑以下代码:

the_queue = Queue()

def do_something(arg1, arg2, queue):
    # Do some stuff...
    result = arg1 + arg2

    queue.put(result)

def init_thread(*arguments):
    t = Thread(target=do_something, args=(arguments, the_queue))
    t.start()
    t.join()

init_thread(3,6)

This throws the exception: 这引发了异常:

TypeError: do_something() takes exactly 3 arguments (2 given)

In other words, the "arguments" tuple is evaluated as one tuple object (ie it's not unpacked), and the_queue is regarded as the second argument. 换句话说,“arguments”元组被评估为一个元组对象(即它没有被解包),而the_queue被认为是第二个参数。

The code needs to be able to initalize threads calling different methods with an unknown number of arguments, but that always will have a "queue" parameter at the end. 代码需要能够使用未知数量的参数初始化调用不同方法的线程,但是最后总是会有一个“queue”参数。

Is there any way to achieve this? 有没有办法实现这个目标? In that case, how? 在那种情况下,怎么样? And if not -- what am I doing wrong? 如果没有 - 我做错了什么?

Thanks. 谢谢。

EDIT: I should add that calling the "init_thread()" method with the queue as an argument is not an option, since I don't want the rest of my code to be "aware" of how the thread handler works internally... 编辑:我应该补充说,使用队列作为参数调用“init_thread()”方法不是一个选项,因为我不希望我的其余代码“了解”线程处理程序如何在内部工作。 。

你需要创建一个新的tuple

t = Thread(target = do_something, args = arguments + (the_queue, ))

You can also unpack the packed-up *arguments tuple, like so: 您也可以解压缩打包的*参数元组,如下所示:

>>> def Print(*args):
...     print('I am prepended to every message!', *args)
... 
>>> Print('This', 'has', 'four', 'arguments')
I am prepended to every message! This has four arguments
>>> def Print(*args):
...     print('I am prepended to every message!', args)
... 
>>> Print('This', 'has', 'four', 'arguments')
I am prepended to every message! ('This', 'has', 'four', 'arguments')

So you see, referring to *agruments, as opposed to arguments, will unpack the tuple. 所以你看,参考* agruments,而不是参数,将解包元组。 Thus your code might be: 因此您的代码可能是:

def init_thread(*arguments):  
    t = Thread(target=do_something, args=(*arguments, the_queue))
    t.start()
    t.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Sh包,如何传递未知数量的参数? - Python Sh package, how to pass unknown number of arguments? 如何在python中初始化QT线程 - How to initialize a QT thread in python 如何初始化先验未知数量的列表 - How to initialize an a priori unknown number of list 如何在 python 的线程中传递 arguments - how to pass arguments in thread in python 从 python 调用 vba 宏,其中 arguments 数量未知 - Calling vba macro from python with unknown number of arguments python中的泛型函数 - 调用具有未知数量参数的方法 - generic function in python - calling a method with unknown number of arguments 从“typer”python || 读取多个单词为 arguments 如何使用“typer”“python”从cli读取未知数量的带有空格的字符串 - read multiple words as arguments from "typer" python || how to read a unknown number of strings with spaces from cli using "typer" "python" 如何在python中将参数传递给正在运行的线程 - how to pass arguments to a running thread in python 如何将列表的元素作为python中线程的参数传递 - How to pass elements of a list as arguments for a thread in python 如何将 arguments 传递给 Python 中的线程函数 - How to pass arguments to thread functions in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM