【问题标题】: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】:

    你有两个选择:

    1. 在命令中添加 & 使其在后台运行

    os.system(command &)

    1. 使用队列/子进程:

      
      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()   
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多