添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
博客介绍了 execute_cmd 和 execute_cmd2 两个可执行命令的函数,它们参数相同。参数 cmd 为子进程命令,shell 为 True 时可传入字符串 cmd,还可指定执行命令的目录 cwd。p.wait() 可等待命令执行结束并返回 exit code。同时提到输出信息过多时会造成死锁,此时可用 execute_cmd2。 摘要生成于 ,由 DeepSeek-R1 满血版支持, def execute_cmd(cmd, cwd, shell=True): p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, shell=shell) excode = p.wait() content = p.stdout.read().decode().strip() error = p.stderr.read().decode().strip() return excode, content, error def execute_cmd2(cmd, cwd, shell=True): p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, shell=shell) content, error = p.communicate() excode = p.poll() return excode, content.decode().strip(), error.decode().strip()

execute_cmd 和 execute_cmd2 都可以执行命令,参数是一样的。

参数cmd为子进程命令,默认只能是字符串列表。shell 默认 False,为 True 代表着使用环境变量中默认 shell 来执行 cmd,这种情况下,可以传入字符串 cmd

例如: cmd = ["python3", "manage.py", "runserver", "0:8000"] 启动django命令

当 shell = True 时,cmd可以是字符串,cmd = "git branch"

cwd 是 执行命令所在的目录,cwd = "/root"

p.wait() 是等待 命令执行结束,返回exit code (0-128),exitcode为0时代表命令正常退出,无错误

content 是命令的输出结果,error是命令错误信息输出结果

注意,如果输出信息太多的话,会死锁卡住。。所以这时候我们就用第二种 execute_cmd2

第二张不会造成死锁

Python 中,可以使用 subprocess 模块的 Popen 函数来执行外部命令。例如,要使用 Popen 执行命令 ls -l,可以这样写: import subprocess cmd =['ls', '-l'] p = subprocess . Popen (cmd, stdout= subprocess . PIPE, stderr= subprocess . PIPE) out, err = . . .
subprocess :可以在当前程序中执行其他程序或命令; mmap:提供一种基于内存的进程间通信机制; multiprocessing:提供支持多处理器技术的多进程编程接口,并且接口的设计最大程度地保持了和threading模块的一致,便于理解和使用。   本文主要介绍 subprocess 模块及其提供的 Popen 类,以 . . .
本来收集整理网络上相关资料后整理: 从 python 2 . 4版本开始,可以用 subprocess 这个模块来产生子进程,并连接到子进程的标准输入/输出/错误中去,还可以得到子进程的返回值。 subprocess 意在替代其他几个老的模块或者函数,比如:os . system os . spawn* os . popen * popen 2 . * commands . * 一、 subprocess . . .
子进程的终止 首先来看一段代码: p = subprocess . Popen (['echo','helloworl . py'], stdin= subprocess . PIPE, stdout= subprocess . PIPE, stderr= subprocess . PIPE) print(p . poll()) print('Exit code:', . . .
1 . 前言 在之前的文章中,实现了Electron-vue在不同系统打包成安装程序。但这只是前端build/package之后的文件打包,虽然服务端的编译之后的exe文件也可以放到一起打包,并且可以去 启动 服务端程序。然而不能与服务端通信的话,那么这个程序存在的意义就不大。所以在这片文章中会讲一下怎么在安装之后, 启动 应用程序调用服务端程序,同时获取服务端的输出值/返回值。 由于不是传统意义上的前后端通信(常见的前后端通信,只需要使用http/https进行通信即可,request发送请求,response返回请
所以新手使用celery很仔细的建立文件夹名字、文件夹层级、python文件名字。 所以网上的celery博客教程虽然很多,但是并不能学会使用,因为要运行起来需要以下6个方面都掌握好,博客文字很难表达清楚或者没有写全面以下6个方面。 celery消费任务不执行或者报错NotRegistered,与很多方面有关系,如果要别人排错,至少要发以下6方面的截图,因为与一下6点关系很大。 1)整个项目目录结构, 2)@task入参 ,3)celery的配置,4)celery的配置 include ,5)cmd命令行启动参数 --queues= 的值,6)用户在启动cmd命令行时候,用户所在的文件夹。 在不规范的文件夹路径下,使用celery难度很高,一般教程都没教。 [项目文件夹目录格式不规范下的celery使用演示](https://github.com/ydf0509/celery_demo) 。 此国产分布式函数调度框架 funboost python万能通用函数加速器 https://funboost.readthedocs.io/zh-cn/latest/articles/c1.html , 从用法调用难度,用户所需代码量,超高并发性能,qps控频精确程度,支持的中间件类型,任务控制方式,稳定程度等20个方面全方位超过celery。发布性能提高1000%,消费性能提高2000%。 python万能分布式函数调度框架funboost支持python所有类型的并发模式和一切知名消息队列中间件,python函数加速器,只需要一行代码调度任意函数,框架包罗万象,万能编程功能宝典,一统编程思维,与业务不绑定,适用范围广。 pip install funboost