The python implementation starts an external program without blocking the current process

  • 2021-08-17 00:13:12
  • OfStack

python can use the system function in the os module to start external programs.

Under Windows platform, using start command can not block the execution program of the current process. The test code is as follows:

import os

os.system('start calc')

Additional knowledge: Python: Start a large number of sub-processes

I won't talk too much, let's just look at the code ~


#!/usr/bin/env python
# coding:UTF-8 
 
"""
@version: python3.x
@author: Cao Xinjian 
@contact: 617349013@qq.com
@software: PyCharm
@file: 5. Start a large number of child processes .py
@time: 2018/9/18 22:28
""" 
from multiprocessing import Process,Pool
import time,os,random
 
def run(num):
 print(" Child process %d Start ---%s" % (num,os.getpid()))
 start = time.time()
 time.sleep(random.choice([1,2,3]))
 end = time.time()
 #print(end)
 print(" Child process %d End ---%s--- Time consuming %.2f" % (num, os.getpid(),end-start))
 
if __name__ == "__main__":
 print(" Parent process startup ")
 # Create a process pool ,Pool Default to CPU Core number 
 pp = Pool()
 for i in range(8):
  # Create a process and put it into the process pool system 1 Management 
  result = pp.apply_async(run,args=(i,))
 
 # Process pool is being transferred join You must transfer before close That calls the close After that, you can't continue to add new processes 
 pp.close()
 pp.join()
 
 print(" Parent process ends ")
 

Related articles: