Python和管道/调用

时间:2013-06-12 15:03:50

标签: python subprocess

我有一个python脚本,它正在运行的环境中导致一些问题。我被告知它“不会释放它打开的文件管道来读取数据。”我认为问题出在最后一行。我想确保我做出正确的更改。这是在Python 2.7环境中运行谢谢。

#!/usr/bin/env python

import subprocess
import urllib
from xml.dom import minidom

ZIPCODE = '06840'
TEMP_TYPE = 'f'
HVAC_ZONES = ['HVAC']
TSTAT = 5


WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=' + ZIPCODE +'&u=' + TEMP_TYPE
WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'

dom = minidom.parse(urllib.urlopen(WEATHER_URL))
ycondition = dom.getElementsByTagNameNS(WEATHER_NS, 'condition')[0]
CURRENT_OUTDOOR_TEMP = ycondition.getAttribute('temp')

for zone in HVAC_ZONES:
   i =0
   while i < TSTAT:
       i += 1
       subprocess.Popen(['/Users/RPM/Applications/RacePointMedia/sclibridge','writestate', zone + '.HVAC_controller.ThermostatCurrentRemoteTemperature'+ '_' + str(i),CURRENT_OUTDOOR_TEMP + TEMP_TYPE.upper()], stdin=subprocess.PIPE)

我知道我需要添加一个关闭,但不要确保我没有任何其他内存泄漏。我需要添加

吗?
subprocess.close

这是使用subprocess.call的更好方法吗?你需要释放/关闭subprocess.call()吗?

#!/usr/bin/env python

import subprocess
import urllib
from xml.dom import minidom

ZIPCODE = '06457'
TEMP_TYPE = 'f'  # f - farhenheit c- celsius (case sensative)
HVAC_ZONES = ['HVAC', 'HVAC2']
TSTAT = 64


WEATHER_URL = 'http://xml.weather.yahoo.com/forecastrss?p=' + ZIPCODE +'&u=' + TEMP_TYPE
WEATHER_NS = 'http://xml.weather.yahoo.com/ns/rss/1.0'

dom = minidom.parse(urllib.urlopen(WEATHER_URL))
ycondition = dom.getElementsByTagNameNS(WEATHER_NS, 'condition')[0]
CURRENT_OUTDOOR_TEMP = ycondition.getAttribute('temp')
print(CURRENT_OUTDOOR_TEMP)

for zone in HVAC_ZONES:
   i =0
   while i < TSTAT:
       i += 1
       command = ['/Users/RPM/Applications/RacePointMedia/sclibridge','writestate', zone + '.HVAC_controller.ThermostatCurrentRemoteTemperature'+ '_' + str(i),CURRENT_OUTDOOR_TEMP + TEMP_TYPE]    
       subprocess.call(str(command),shell=True)

修改

好的我已经用建议重写了这个但是我仍然需要shell = True见下面

#!/usr/bin/env python

#define imports

import sys
import subprocess
import os
from subprocess import STDOUT

DEVNULL = open(os.devnull, "r+b")

#start global definitions
command = ['/Users/RPM/Applications/RacePointMedia/sclibridge servicerequest "Wine Cellar" "" "" "1" "" "Pause"']
#start main program
subprocess.call(command, close_fds=True, stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT,shell=True)

我在没有shell = True的情况下尝试了这个,我得到以下错误,我主要担心的是内存泄漏。

 File "./test.py", line 14, in <module>
    subprocess.call(commamd,close_fds=True)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 493, in call
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child

1 个答案:

答案 0 :(得分:2)

要一次运行一个子流程,您可以使用subprocess.call作为@tdelaney建议:

#..
call(command, close_fds=True,
     stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT)

您不需要shell=Truesubprocess会直接执行sclibridge

  • close_fds=True是为了避免从父级继承其他打开的文件描述符。
  • stdinstdoutstderr设置为提供空输入并放弃任何输出

一次以并行nconcurrent运行子进程而不从子进程中泄漏文件描述符:

import os
from subprocess import STDOUT, call
from multiprocessing.dummy import Pool # use threads

DEVNULL = open(os.devnull, "r+b")
# define HVAC_ZONES, TSTAT, CURRENT_OUTDOOR_TEMP, TEMP_TYPE here
#..
file_pattern = '.HVAC_controller.ThermostatCurrentRemoteTemperature'+ '_' 
def run(zone_i):
    zone, i = zone_i
    cmd = ['/Users/RPM/Applications/RacePointMedia/sclibridge',
           'writestate', 
           zone + file_pattern + str(i),
           CURRENT_OUTDOOR_TEMP + TEMP_TYPE]
    return cmd, call(cmd, close_fds=True,
                     stdin=DEVNULL, stdout=DEVNULL, stderr=STDOUT) 

nconcurrent = 20 # limit number of concurrent processes
commands = ((zone, i+1) for zone in HVAC_ZONES for i in range(TSTAT))
pool = Pool(nconcurrent)
for cmd, returncode in pool.imap_unordered(run, commands):
    if returncode != 0:
        print("failed with returncode: %d, cmd: %s" % (returncode, cmd))
pool.close()
pool.join()
DEVNULL.close()