python-3.x - [错误] : Issue with threading. Thread() 在使用 Python3 执行多线程脚本时

标签 python-3.x multithreading

下面的代码示例是我正在尝试创建的一个小脚本。在这个脚本中,有两个函数。 calculate_the_square(takes_the_given_array)calculate_the_cube(takes_the_given_array)

我创建了两个线程,threa_01 和 thread_02,我告诉他们执行我想要的函数。在 args 变量中,我发送要发送的数组以便计算(args=array)。在目标变量中,我发送要在其中运行的特定线程的函数的名称(target=calculate_the_squaretarget=calculate_the_cube)。然后我开始执行线程,并让它们加入。

不幸的是,当我执行它时,我遇到了以下问题:

问题

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: calculate_the_square() takes 1 positional argument but 6 were given

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\Program Files (x86)\Python37-32\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
TypeError: calculate_the_cube() takes 1 positional argument but 6 were given


Process finished with exit code 0

WANTED OUTPUT(它必须混合,但我写的是通用结果而不是多线程)

Cube result:  5832
Cube result:  778688
Cube result:  2000376
Cube result: 281011375 
Cube result:  967361669
Cube result:  1006012008
Square result:  324 
Square result:  8464
Square result:  15876
Square result:  429025
Square result:  978121
Square result:  1004004
The program finished in : ...... SOME TIME

代码示例

import time
import threading

def calculate_the_square(variables):

    for var in variables:
        time.sleep(0.5)     # Insert a sleep just to count the time that needs to be completed
        print("Square result: ", var*var)

def calculate_the_cube(variables):
    time.sleep(0.5)     # Insert a sleep just to count the time that needs to be completed
    for var in variables:
        print("Cube result: ", var*var*var)


keeping_time = time.time()              # Keeping the starting time
array = [18, 92, 126, 655, 989, 1002]  # Random given numbers.



thread_01 = threading.Thread(target=calculate_the_square, args=(array))     # Creating threadh No1 and the target make this thread to focus on this function, sending the value that is saved in tha variable args.
thread_02 = threading.Thread(target=calculate_the_cube, args=(array))       # Creating threadh No2 and the target make this thread to focus on this function, sending the value that is saved in tha variable args.


thread_01.start()   # Starting the thread 1
thread_02.start()   # Starting the thread 2

thread_01.join() # Waits until the first thread is finished. Better switching between the threads. Maybe not now (only 2 threads) but if we had 10 and more it would be helpful.
thread_02.join()

print("The program finished in :", time.time()-keeping_time)

你能帮我解决这个问题吗?我做错了什么?

提前感谢您的宝贵时间!

最佳答案

我将使用第一个答案中的内容,我会说:

  • 什么是元组,它的作用是什么:元组类似于列表。两者的区别在于,一旦分配了元组,我们就无法更改其元素,而在列表中,元素可以更改。
  • 元组的优点:1)我们通常将元组用于异构(不同)数据类型,将列表用于同质(相似)数据类型,2)由于元组是不可变的,因此对元组进行迭代是比使用列表更快。所以性能略有提升,3)包含不可变元素的元组可以用作字典的键。使用列表,这是不可能的,4)如果您有不变的数据,将其实现为元组将保证它保持写保护

所以从我所见,您在理解多线程逻辑方面没有问题,但您无法在代码中看到元组的原因。现在我觉得很清楚了。当然,第一个答案的解决方案是正确的。逗号足以声明您不想在此函数中传递任何内容:

thread_01 = threading.Thread(target=calculate_the_square, args=(array,))
thread_02 = threading.Thread(target=calculate_the_cube, args=(array,))

我看到了第一个答案的资源链接。我也会告诉你看看这里。我认为它更有用,更容易理解大局。

再见!

关于python-3.x - [错误] : Issue with threading. Thread() 在使用 Python3 执行多线程脚本时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54750137/

相关文章:

c++ - 中断 boost::asio 同步读取?

java - 并发 api 的 AbstractQueuedSynchronizer 构建 block 如何没有任何同步?

Python 3 - 从 beautifulSoup 中的标签获取文本

python - 如何使用 Pandas 删除基于特定列的重复值?

python - 将 JSON 导入 pandas 数据框错误

java - 一个线程可以直接让另一个线程等待吗?

python-3.x - 如何在管道中使用适当的 FunctionTransformer 制作 GridSearchCV?

python - 用于确定使用了哪个参数解包(位置或关键字)的内省(introspection)函数

java - 为什么ThreadLocal不在新线程中创建?

java - 执行者是否使用创建它们的线程?