python多线程,执行第10个时退出了,为啥啊?

ning100_csdn 2018-07-05 03:32:21
import threading
import time
def tpppppp(qqq):
print (threading.current_thread().name,threading.activeCount(),qqq)
time.sleep(1)
pppp=0
print (type(pppp))
print (threading.current_thread().getName())
while 1:
if threading.activeCount() <= 4:
pppp += 1
tt=threading.Thread(target = tpppppp,args = (str(pppp)))
tt.setDaemon(True)
tt.start()
tt.join()

=====输出如下,第10个就退出了=============
MainThread
Thread-1 2 1
Thread-2 2 2
Thread-3 2 3
Thread-4 2 4
Thread-5 2 5
Thread-6 2 6
Thread-7 2 7
Thread-8 2 8
Thread-9 2 9
Exception in thread Thread-10:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: tpppppp() takes 1 positional argument but 2 were given

Exception in thread Thread-11:
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 916, in _bootstrap_inner
self.run()
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
TypeError: tpppppp() takes 1 positional argument but 2 were given
...全文
337 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ning100_csdn 2018-07-10
  • 打赏
  • 举报
回复
牛人啊,谢谢啦
ghostgoodbye 2018-07-06
  • 打赏
  • 举报
回复
解决方法:args=(pppp,)
至于为什么str(pppp)能变为元祖数据,这个还请高人解答?
难道str的数据本身就是元祖形式的吗????
ghostgoodbye 2018-07-06
  • 打赏
  • 举报
回复
看看thread的构造函数:
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, verbose=None):
args接受一个()类型的数据,此处就是str(pppp)
看看target的调用过程:
if self.__target:
self.__target(*self.__args, **self.__kwargs)
将*self.__args作为参数传递过去,即*str(pppp)。
当参数达到两位数时,比如10,拆分成1,0两个参数。所以报错:TypeError: tpppppp() takes 1 positional argument but 2 were given

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧