简体   繁体   English

在Python中异步执行命令

[英]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,但对此有所怀疑。

  • how many simultaneous popen calls can be fired. 可以触发多少个同时的Popen呼叫。
  • does the result immediately come in stdout? 结果立即出现在stdout中吗? (for now it looks the case but how to ensure it if the commands take long time) (目前看来,但是如果命令花费很长时间该如何确保)
  • how to ensure that everything is async - keeping close to single thread model? 如何确保一切都是异步的-保持接近单线程模型?

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.listdiros.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM