简体   繁体   English

如何在 python 中使用 os.system() 同时执行多个命令

[英]How to execute multiple commands simultaneously using os.system() in python

import os  
import sys

command_0 = './main'
command_1 = './main'

os.system(command_0)
os.system(command_1)

I want to run a script called 'main' twice simultaneously using python, the problem is that command_0 run for 5 seconds and only when it finishes the command_1 is called.我想使用 python 同时运行两次名为“main”的脚本,问题是 command_0 运行 5 秒,并且只有在它完成时才调用 command_1。 Is it possible to run both simultaneously?是否可以同时运行两者?

you have 2 options:你有两个选择:

  1. add & to the command so it will run in the background在命令中添加 & 使其在后台运行

os.system(command &)

  1. use queue / subprocess:使用队列/子进程:

     def worker(): while True: item = q.get() os.system(item) q = Queue() for i in tasks: t = Thread(target=worker) t.setDaemon(True) t.start() for item in tasks: q.put(item) q.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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