subprocess 文件描述符111

import os
import stat

_fd_types = (
(‘REG’, stat.S_ISREG),
(‘FIFO’, stat.S_ISFIFO),
(‘DIR’, stat.S_ISDIR),
(‘CHR’, stat.S_ISCHR),
(‘BLK’, stat.S_ISBLK),
(‘LNK’, stat.S_ISLNK),
(‘SOCK’, stat.S_ISSOCK)
)

def fd_table_status():
result = []
for fd in range(100):
try:
s = os.fstat(fd)
except:
continue
for fd_type, func in _fd_types:
if func(s.st_mode):
break
else:
fd_type = str(s.st_mode)
result.append((fd, fd_type))
return result

def fd_table_status_logify(fd_table_result):
return ('Open file handles: ’ +
‘, ‘.join([’{0}: {1}’.format(*i) for i in fd_table_result]))

def fd_table_status_str():
return fd_table_status_logify(fd_table_status())

if name==‘main’:
print fd_table_status_str()

import fd_table_status
import subprocess
import platform

fds = fd_table_status.fd_table_status_str

if platform.system()==‘Windows’:
python_exe = r’C:\Python27\python.exe’
else:
python_exe = ‘python’

print ‘1) Initial file descriptors:\n’ + fds()
f = open(‘fd_table_status.py’, ‘r’)
print ‘2) After file open, before Popen:\n’ + fds()
p = subprocess.Popen([‘python’, ‘fd_table_status.py’],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
print ‘3) After Popen, before reading piped output:\n’ + fds()
result = p.communicate()
print ‘4) After Popen.communicate():\n’ + fds()
del p
print ‘5) After deleting reference to Popen instance:\n’ + fds()
del f
print ‘6) After deleting reference to file instance:\n’ + fds()
print ‘7) child process had the following file descriptors:’
print result[0][:-1]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值