鱼C论坛

 找回密码
 立即注册
查看: 2409|回复: 2

python多线程执行命令无法获取到命令

[复制链接]
发表于 2023-10-12 21:45:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import subprocess
  2. import threading

  3. # 定义一个函数,用于执行一个命令,并打印输出和错误信息
  4. def run_command(cmd):
  5.     # 创建一个子进程,执行命令,并捕获标准输出和标准错误
  6.     process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  7.     # 读取输出和错误信息
  8.     output, error = process.communicate()
  9.     # 打印输出和错误信息
  10.     print(f"Output of {cmd}:")
  11.     print(output.decode())
  12.     print(f"Error of {cmd}:")
  13.     print(error.decode())

  14. # 定义一个列表,存储要执行的命令
  15. commands = ["ls"]

  16. # 创建一个空列表,用于存储线程对象
  17. threads = []

  18. # 遍历命令列表,为每个命令创建一个线程,并启动线程
  19. for cmd in commands:
  20.     # 创建一个线程对象,指定目标函数和参数
  21.     thread = threading.Thread(target=run_command, args=(cmd))
  22.     # 将线程对象添加到线程列表中
  23.     threads.append(thread)
  24.     # 启动线程
  25.     thread.start()
  26. # 遍历线程列表,等待每个线程结束
  27. for thread in threads:
  28.     # 等待线程结束
  29.     thread.join()
复制代码


报错的log已经附了截图,应该是无法解析ls这个命令,这是为什么呢,怎么修改

ls这个命令无法获取到

ls这个命令无法获取到

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-10-12 21:46:06 | 显示全部楼层
问题出在  args  参数上,应该将参数作为一个元组传递进去,即  (cmd,) 。修改后的代码如下:

  1. import subprocess
  2. import threading

  3. # 定义一个函数,用于执行一个命令,并打印输出和错误信息
  4. def run_command(cmd):
  5.     # 创建一个子进程,执行命令,并捕获标准输出和标准错误
  6.     process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  7.     # 读取输出和错误信息
  8.     output, error = process.communicate()
  9.     # 打印输出和错误信息
  10.     print(f"Output of {cmd}:")
  11.     print(output.decode())
  12.     print(f"Error of {cmd}:")
  13.     print(error.decode())

  14. # 定义一个列表,存储要执行的命令
  15. commands = ["ls"]

  16. # 创建一个空列表,用于存储线程对象
  17. threads = []

  18. # 遍历命令列表,为每个命令创建一个线程,并启动线程
  19. for cmd in commands:
  20.     # 创建一个线程对象,指定目标函数和参数
  21.     thread = threading.Thread(target=run_command, args=(cmd,))
  22.     # 将线程对象添加到线程列表中
  23.     threads.append(thread)
  24.     # 启动线程
  25.     thread.start()

  26. # 遍历线程列表,等待每个线程结束
  27. for thread in threads:
  28.     # 等待线程结束
  29.     thread.join()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-10-12 21:46:36 | 显示全部楼层
问题分析:
根据您提供的代码和错误信息,发现ls命令无法被正确解析。原因是在创建线程对象时,参数列表没有使用元组或列表进行包装,导致命令无法正确传递。

解决方案:
要解决这个问题,可以按照以下步骤修改代码:

1. 将args=(cmd)修改为args=(cmd,),即将命令参数包装成一个元组。
   这样可以确保命令参数正确传递给target函数。

2. 更新修改后的代码如下:
  1. import subprocess
  2. import threading
  3. def run_command(cmd):
  4.     process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  5.     output, error = process.communicate()
  6.     print(f"Output of {cmd}:")
  7.     print(output.decode())
  8.     print(f"Error of {cmd}:")
  9.     print(error.decode())
  10. commands = ["ls"]
  11. threads = []
  12. for cmd in commands:
  13.     thread = threading.Thread(target=run_command, args=(cmd,))
  14.     threads.append(thread)
  15.     thread.start()
  16. for thread in threads:
  17.     thread.join()
复制代码
请注意,上述修改后的代码已经对参数进行了包装,并正确传递给了target函数。

尝试运行修改后的代码,看是否能够成功执行命令并打印输出信息。

希望以上解决方案能够帮助您解决问题。如果您还有其他问题,请随时提问。

球一个最佳答案谢谢啦!这对我非常重要!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 17:45

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表