*后的Python线程模块错误参数必须是可迭代的,而不是int

Meh*_*bla 0 python multithreading module

所以我在玩线程模块以了解它的基础知识,当我运行我的代码时,我收到一个错误,没有明确说明我做错了什么。这是代码,在下面,您可以找到错误日志:

import threading
import time

start = time.perf_counter()


def square(y):
    print('Starting processing')
    for x in range(y):
        i = x * x
    print(f'Done processing {i}')

threads = []

# creating thread
for _ in range(10):
    thread = threading.Thread(target=square, args=1000000)
    # starting thread
    thread.start()
    threads.append(thread)

# makes sure that the threads complete before moving to the rest of the code
for i in threads:
    i.join()




finish = time.perf_counter()

print(f'Done processing in {round(finish - start, 4)} second(s).')
Run Code Online (Sandbox Code Playgroud)

我得到这个错误:

Exception in thread Thread-1:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in 
    runself._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-3:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, 
    in_bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in 
    runself._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-4:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_inner
self.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in 
    runself._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-2:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int
Exception in thread Thread-5:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run 
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int


Exception in thread Thread-6:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-7:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-8:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-9:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int

Exception in thread Thread-10:
Traceback (most recent call last):
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in 
    _bootstrap_innerself.run()
    File "C:\Users\EM\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: square() argument after * must be an iterable, not int
Run Code Online (Sandbox Code Playgroud)

我不知道我的代码有什么问题,错误日志也没有指出错误来自哪里。

Van*_*boy 7

尝试

thread = threading.Thread(target=square, args=(1000000,))
Run Code Online (Sandbox Code Playgroud)

该函数需要一个元组/列表参数,它是可迭代的,所以如果你有更多的参数要传递,你只需像这样 args=(param1, param2, param3)。