简体   繁体   English

Python线程模块无法识别args

[英]Python threading module not recognizing args

For some reason, the folowing error is being thrown even though the one required argument is given. 由于某些原因,即使给出了一个必需的参数,也会引发以下错误。

Traceback (most recent call last):
    File "C:\Users\josep_000\Desktop\test.py", line 53, in <module>
        a=threading.Thread(target=getin(),args=(aa))
TypeError: getin() missing 1 required positional argument: 'var'

Remove the parentheses after getin() : getin()之后删除括号:

    a=threading.Thread(target=getin(),args=(aa))
                                   ^^ these need to be removed

Currently, your code calls getin() directly instead of passing it to the Thread constructor to be called from the context of the new thread. 当前,您的代码直接调用getin() ,而不是将其传递给Thread构造函数以从新线程的上下文中进行调用。

Also, args needs to be a tuple. 另外, args必须是一个元组。 To create a single-element tuple, add a comma after aa : 要创建单元素元组,请在aa之后添加一个逗号:

    a = threading.Thread(target=getin, args=(aa,))

Python threading.Thread accepts callable object as a target. Python threading.Thread接受callable对象作为目标。

So when you do this threading.Thread(target=getin(),args=(aa)) .. Ideally you are passing passing the return value of getin being called with no arguments. 因此,当您执行此threading.Thread(target=getin(),args=(aa)) ..理想情况下,您传递的是传递不带参数的getin的返回值。 Since getin requires 1 argument this is an error. 由于getin需要1个参数,因此这是一个错误。

You should pass like below... 您应该像下面这样通过...

threading.Thread(target=getin,args=(aa, ))

Also, Thread class accepts tuple as args ... 此外, Thread类接受tuple作为args ...

When you use parentheses without any comma inside Python just yields the value given inside....If you need a tuple with one value you've to add a comma inside. 当您在Python内部使用没有任何逗号的parentheses ,只yields内部给定的值。...如果您需要一个带有一个值的元组,则必须在内部添加一个comma

Parentheses without any comma inside gives you a tuple object with empty items like below.. 括号内没有任何逗号,可以为您提供一个tuple object其中包含空项,如下所示。

>>> a = ()
>>> a
()
>>> type(a)
<type 'tuple'>

If you use parentheses having one value inside without any comma you'll endup getting like below.. 如果您使用的parentheses内含一个值而没有任何comma ,则结果将如下所示。

>>> b = (1)
>>> type(b)
<type 'int'>

If you need a tuple having one value you've to add a comma inside..like below. 如果您需要具有一个值的元组,则必须在内部添加一个comma

>>> c = (1,)
>>> type(c)
<type 'tuple'>

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

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