鱼C论坛

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

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

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

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

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

x
import subprocess
import threading

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

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

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

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

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

ls这个命令无法获取到

ls这个命令无法获取到

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

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

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

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

# 遍历线程列表,等待每个线程结束
for thread in threads:
    # 等待线程结束
    thread.join()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

2. 更新修改后的代码如下:
import subprocessimport threadingdef run_command(cmd):    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)    output, error = process.communicate()    print(f"Output of {cmd}:")    print(output.decode())    print(f"Error of {cmd}:")    print(error.decode())commands = ["ls"]threads = []for cmd in commands:    thread = threading.Thread(target=run_command, args=(cmd,))    threads.append(thread)    thread.start()for thread in threads:    thread.join()
请注意,上述修改后的代码已经对参数进行了包装,并正确传递给了target函数。

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

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

球一个最佳答案谢谢啦!这对我非常重要!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 10:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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