gpt4 book ai didi

python - Python中的多线程问题

转载 作者:行者123 更新时间:2023-11-28 22:38:02 25 4
gpt4 key购买 nike

刚接触 Python 多线程并编写如此简单的程序,这是我的代码和错误消息,有什么想法有什么问题吗?谢谢。

使用 Python 2.7。

import time
import thread

def uploader(threadName):

while True:
time.sleep(5)
print threadName

if __name__ == "__main__":

numOfThreads = 5
try:
i = 0
while i < numOfThreads:
thread.start_new_thread(uploader, ('thread'+str(i)))
i += 1

print 'press any key to exit test'
n=raw_input()

except:
print "Error: unable to start thread"

Unhandled exception in thread started by <pydev_monkey._NewThreadStartupWithTrace instance at 0x10e12c830>
Traceback (most recent call last):
File "/Applications/PyCharm CE.app/Contents/helpers/pydev/pydev_monkey.py", line 521, in __call__
return self.original_func(*self.args, **self.kwargs)
TypeError: uploader() takes exactly 1 argument (7 given)

提前致谢,林

最佳答案

thread.start_new_thread 的参数 need to be a tuple .而不是这个:

('thread' + str(i))  # results in a string

试试这个参数:

('thread' + str(i),)  # a tuple with a single element

顺便说一句,你应该查看 threading module ,这是比 thread 更高级别的接口(interface)。

关于python - Python中的多线程问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35965190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com