Python错误集锦:开启线程时提示TypeError: func() argument after * must be an iterable, not int

原文链接:http://www.juzicode.com/archives/2756

错误提示:

threading.Thread()开启线程时提示TypeError: func() argument after * must be an iterable, not int

#juzicode.com/vx:桔子code
import  threading
def func(para1):
    print(para1)
if __name__ == '__main__':
    para1 =11
    t0 = threading.Thread(target=func, name='func-0',args=(para1))
    t0.start()
Exception in thread func-0:
Traceback (most recent call last):
  File "d:\python\python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "d:\python\python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
TypeError: func() argument after * must be an iterable, not int

错误原因:

1、使用threading.Thread()创建线程时,args参数传递的是一个元组,即使这个时候函数只有1个参数。

解决方法:

1、改用元组传递参数,t0 = threading.Thread(target=func, name=’func-0′,args=(para1,)) ,para1之后有个逗号”,”

#juzicode.com/vx:桔子code
import  threading
def func(para1):
    print(para1)
if __name__ == '__main__':
    para1 =1234567890
    t0 = threading.Thread(target=func, name='func-0',args=(para1,))
    t0.start()
1234567890

为什么要增加逗号,可以参考 Python进阶教程m10–多线程好冷的Python–tuple和逗号的恩怨情仇

相关内容:

  1. Python进阶教程m10–多线程
  2. Python进阶教程m10b–多线程通信 

关注微信公众号:“桔子code”,欢迎后台留言撩我,我会尽我所能为你解惑Python,C等编程知识

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注