Python的線程&進程&協程[2] -> 進程 -> 多進程的基本使用

來源:互聯網
上載者:User

標籤:ring   span   函數   log   子進程   string   ica   tar   pip   

多進程的基本使用

 

1 subprocess 常用函數樣本

首先定義一個子進程調用的程式,用於列印一個輸出語句,並擷取命令列參數

1 import sys2 print(‘Called_Function.py called, Hello world.‘)3 try:4     print(‘Got para‘, sys.argv[1:])5 except:6     pass

再定義主函數,即父進程,分別測試 run() / call() / check_call() / getstatusoutput() / getoutput() / ckeck_output函數。

 1 import subprocess 2  3 # subprocess.run() 4 print(‘---------subprocess.run------------‘) 5 re = subprocess.run([‘python‘, ‘Called_Function.py‘, ‘para_1‘, ‘para_2‘]) 6 print(‘subprocess.run() test returns obj:‘, re) 7 print(‘Return code is: {0}, stdout is: {1}, stderr is: {2}‘.format(re.returncode, re.stdout, re.stderr)) 8  9 # subprocess.call()10 print(‘\n---------subprocess.call------------‘)11 print(‘subprocess.call() test returns code:‘, subprocess.call([‘python‘, ‘Called_Function.py‘, ‘para_1‘, ‘para_2‘]))12 13 # subprocess.ckeck_call()14 print(‘\n---------subprocess.check_call------------‘)15 try:16     print(‘subprocess.check_call() test returns code:‘, subprocess.check_call([‘python‘, ‘Called_Function.py‘]))17 except subprocess.CalledProcessError:18     print(‘Failed to call.‘)19 20 # subprocess.getstatusoutput()21 print(‘\n---------subprocess.getstatusoutput------------‘)22 print(‘subprocess.getstatusoutput() test returns:‘, subprocess.getstatusoutput([‘python‘, ‘Called_Function.py‘]))23 24 # subprocess.getoutput()25 print(‘\n---------subprocess.getstatusoutput------------‘)26 print(‘subprocess.getoutput() test returns:‘, subprocess.getoutput([‘python‘, ‘Called_Function.py‘]))27 28 # subprocess.check_output()29 print(‘\n---------subprocess.check_output------------‘)30 print(‘subprocess.check_output() test returns:‘, subprocess.check_output([‘python‘, ‘Called_Function.py‘]))

 

2 利用Popen類與子進程互動

首先定義一個子進程調用的函數,函數中需求一個輸入

1 x = input(‘Please input something.‘)2 print(x, ‘Hello World!‘)3 # Raise an error4 print(y)

再定義父進程的函數

 1 import subprocess 2  3 prcs = subprocess.Popen([‘python‘, ‘Called_Function_Popen.py‘], 4                         stdout=subprocess.PIPE, 5                         stdin=subprocess.PIPE, 6                         stderr=subprocess.PIPE, 7                         universal_newlines=True, 8                         shell=True) 9 10 print(‘subprocess pid:‘, prcs.pid)11 12 re = prcs.communicate(‘These string are from stdin‘)13 print(‘\nSTDOUT:‘, re[0])14 print(‘\nSTDERR:‘, re[1])15 if prcs.poll():16     print(‘\nThe subprocess has been done‘)

相關閱讀

1. subprocess 模組

 

Python的線程&進程&協程[2] -> 進程 -> 多進程的基本使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.