【问题标题】:How to execute multiple commands simultaneously using os.system() in python如何在 python 中使用 os.system() 同时执行多个命令
【发布时间】:2022-01-13 15:56:22
【问题描述】:
import os
import sys
command_0 = './main'
command_1 = './main'
os.system(command_0)
os.system(command_1)
我想使用 python 同时运行一个名为“main”的脚本两次,问题是 command_0 运行 5 秒,只有当它完成时才会调用 command_1。可以同时运行吗?
【问题讨论】:
标签:
python
unix
operating-system
command
【解决方案1】:
你有两个选择:
- 在命令中添加 & 使其在后台运行
os.system(command &)
-
使用队列/子进程:
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()