简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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