2012-12-12 5 views
7

पर कॉल करें मैं यह समझने की कोशिश कर रहा था कि यह क्यों हो रहा है। मैं उबंटू सर्वर 12.04 पर नेटवर्किंग को पुनरारंभ करने के लिए कमांड को कॉल कर रहा हूं।पायथन उपप्रोकैस check_output बहुत धीमी है तो

फास्ट निष्पादन

जब मैं कमांड तीन तरीके यह निष्पादित करने के लिए चारों ओर 0.1 सेकंड लेता है निम्न में से एक का उपयोग कर फोन:

टर्मिनल
  • अजगर स्क्रिप्ट में
      सीधे
    1. का उपयोग कर os.system
    2. subprocess.call
    का उपयोग कर पायथन लिपि 0

    टर्मिनल सत्र:

    [email protected]:~# time /etc/init.d/networking restart 
    * Running /etc/init.d/networking restart 
    * Reconfiguring network interfaces... 
    real 0m0.105s 
    
    [email protected]:~# time python -c "import os; 
    > os.system('/etc/init.d/networking restart')" 
    * Running /etc/init.d/networking restart 
    * Reconfiguring network interfaces... 
    real 0m0.111s 
    
    [email protected]:~# time python -c "import subprocess; 
    > subprocess.call(['/etc/init.d/networking', 'restart'])" 
    * Running /etc/init.d/networking restart 
    * Reconfiguring network interfaces... 
    real 0m0.111s 
    

    धीरे निष्पादन

    लेकिन अगर मैं subprocess.check_output या popen का उपयोग करें और कोशिश करते हैं और उत्पादन में यह 23 सेकंड लेता है पढ़ें। रास्ता धीमा ऐसा लगता है कि यह नाटकीय अंतर केवल तब होता है जब मैं किसी फ़ंक्शन का प्रयास करता हूं और उपयोग करता हूं जो कमांड आउटपुट लौटाएगा। मैं समझना चाहता हूं कि यह क्यों हो रहा है और इस आदेश को निष्पादित करने का समाधान ढूंढें और इसे बिना आउटपुट के आउटपुट प्राप्त करें।

    टर्मिनल सत्र:

    [email protected]:~# time python -c "import subprocess; 
    > print subprocess.check_output(['/etc/init.d/networking', 'restart'])" 
    * Running /etc/init.d/networking restart 
    * Reconfiguring network interfaces... 
    real 0m23.201s 
    
    [email protected]:~# time python -c "from subprocess import Popen, PIPE; 
    > print Popen(['/etc/init.d/networking', 'restart'], stdout=PIPE).stdout.read()" 
    * Running /etc/init.d/networking restart 
    * Reconfiguring network interfaces... 
    real 0m23.201s 
    

    अद्यतन

    टिप्पणियों में से एक टी आदेश बाहर की कोशिश कर रहा सुझाव दिया। परिणाम जहां बहुत दिलचस्प है। यदि टर्म का उपयोग किया जाता है तो पाइथन की किसी भी भागीदारी के बिना टर्मिनल में यह 23 सेकंड लेता है। मैं अभी भी उत्सुक हूं कि क्यों कम से कम यह एक सुराग दे सकता है कि क्या हो रहा है।

    [email protected]:~# time /etc/init.d/networking restart | tee out.txt 
    * Running /etc/init.d/networking restart 
    * Reconfiguring network interfaces... 
    real 0m23.181s 
    
  • +0

    यदि निम्न में से कोई प्रासंगिक है मैं नहीं जानता कि ([प्रश्न # 10,150,368] (http://stackoverflow.com/questions/10150368/why-is-piping-output-of-subprocess-so -unreliable-with-python), [प्रश्न # 4940607] (http://stackoverflow.com/questions/4940607/python-subprocesses- अनुभव- रहस्यमय-delay-in-receiving-stdin-eof)) लेकिन एक जवाब जोड़ने का सुझाव देता है 'close_fds = True' पैरामीटर पैरामीटर –

    +0

    'subprocess.call()' केवल 'subprocess.Popen (* popenargs, ** kwargs) .wait()'। – Blender

    +0

    @ jwpat7 लिंक के लिए धन्यवाद। मैंने close_fds = True की कोशिश की, यह कोई फर्क नहीं पड़ता। –

    उत्तर

    8

    नीचे दिया गया कोड उत्कृष्ट टिप्पणी जेएफ सेबस्टियन पर आधारित है। नीचे दिया गया कोड 0.1 सेकंड में अपेक्षित के रूप में चलता है और कमांड के आउटपुट को स्ट्रिंग पर देता है।

    from subprocess import check_call, STDOUT 
    from tempfile import NamedTemporaryFile 
    
    with NamedTemporaryFile() as f: 
        check_call(['/etc/init.d/networking', 'restart'], stdout=f, stderr=STDOUT) 
        f.seek(0) 
        output = f.read()