[报错] TypeError: run() argument after * must be an iterable, not int

左手的ㄟ右手 2022-11-21 04:07 281阅读 0赞

[报错] TypeError: run() argument after * must be an iterable, not int

问题描述:

在多线程操作,调用threading.Thread 报错,TypeError: run() argument after * must be an iterable, not int

  1. import threading
  2. def run(n):
  3. print("run the thread:",n)
  4. num = 0
  5. th1 = threading.Thread(target=run, args=(1))
  6. th1.start()

报错:TypeError: run() argument after * must be an iterable, not int

报错原因:

在使用多线程时,会调用多线程类中的run()函数,这个函数需要传入一个可迭代对象,当我们的参数只有一个整数时,单独的整数不可迭代,所以报错;

解决办法:

在整数后面加一个逗号(,) 使其变成元组,元组可迭代,问题解决!

  1. import threading
  2. def run(n):
  3. print("run the thread:",n)
  4. num = 0
  5. th1 = threading.Thread(target=run, args=(1,)) # 在参数1 后面加一个, 即可解决异常;
  6. th1.start()

问题解决!!

发表评论

表情:
评论列表 (有 0 条评论,281人围观)

还没有评论,来说两句吧...

相关阅读