[英]Async execution of commands in Python
Requirement - I want to execute a command that uses ls, grep, head etc using pipes (|). 要求-我想通过管道(|)执行使用ls,grep,head等命令。 I am searching for some pattern and extracting some info which is part of the query my http server supports. 我正在搜索某种模式并提取一些信息,这是我的http服务器支持的查询的一部分。
The final output should not be too big so m assuming stdout should be good to use (I read about deadlock issues somewhere) Currently, I use popen from subprocess module but I have my doubts over it. 最终输出不应太大,因此,假设stdout应该很好用(我在某处读到了死锁问题),目前,我在子流程模块中使用popen,但对此有所怀疑。
I am new to Python and links to videos/articles are also appreciated. 我是Python的新手,也欢迎您链接到视频/文章。 Any other way than popen is also fine. 除了popen以外的任何其他方式都可以。
You could use os.listdir
or os.walk
instead of ls
, and the re
module instead of grep
. 您可以使用os.listdir
或os.walk
代替ls
,并使用re
模块代替grep
。
Wrap everything up in a function, and use eg the map
method from a multiprocessing.Pool
object to run several of those in parallel. 将所有内容包装到一个函数中,然后使用multiprocessing.Pool
对象中的map
方法来并行运行其中的几个。 This is a pattern that works very well. 这是一种非常有效的模式。
In Python3 you can also use Executors
from concurrent.futures
in a similar way. 在Python3中,您还可以类似的方式使用来自concurrent.futures
Executors
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.