python-3.x - [错误]:使用 Python3 执行多线程脚本时出现 threading.Thread() 问题
问题描述
下面的代码示例是我正在尝试创建的一个小脚本。在这个脚本中,有两个函数。calculate_the_square(takes_the_given_array)
和calculate_the_cube(takes_the_given_array)
。_
我创建了两个线程,threa_01 和 thread_02,我告诉他们执行我想要的函数。在 args 变量中,我发送要发送的数组以便计算(args=array)。在目标变量中,我发送要在其中运行的特定线程的函数的名称(target=calculate_the_square
或target=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
想要的输出(它必须混合,但我写的是通用结果而不是多线程)
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)
你能帮我解决这个问题吗?我究竟做错了什么?
提前感谢您的时间!
解决方案
args参数必须是一个元组。你需要改变
thread_01 = threading.Thread(target=calculate_the_square, args=(array))
thread_02 = threading.Thread(target=calculate_the_cube, args=(array))
进入
thread_01 = threading.Thread(target=calculate_the_square, args=(array,))
thread_02 = threading.Thread(target=calculate_the_cube, args=(array,))
您可以通过输入解释器来检查它:
type((1))
type((1,))
输出:
<class 'int'>
<class 'tuple'>
推荐阅读
- python - 通过套接字在 raw_input 变量中发送转义的十六进制字符串不起作用?
- android - 9 路径可绘制背景:错误膨胀类
- vue.js - 如何从 VueJs 的下拉列表中获取选定的值?
- android - ARCore Android Studio:在屏幕的 2D 坐标上放置一个 3D 模型
- bash - 从 rest api 或 task 获取 nexus 工件详细信息
- javascript - 有没有办法为 JavaScript/TypeScript 中数组中的每个数组递归执行 .map?
- rendering - 从四个点获得一个四边形,然后将其分成两个三角形
- java - 为什么从 JMeter OS Sampler 启动的 BAT 文件没有触发 Maven 执行?
- javascript - 如何解决此问题:“node_modules/expo/AppEntry.js: [BABEL]”?
- php - 数据Mysql未显示在recyclerview中