本文整理汇总了Python中io.FileIO.fileno方法的典型用法代码示例。如果您正苦于以下问题:Python FileIO.fileno方法的具体用法?Python FileIO.fileno怎么用?Python FileIO.fileno使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.FileIO
的用法示例。
在下文中一共展示了FileIO.fileno方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_to_file
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import fileno [as 别名]
def write_to_file(file_fd: io.FileIO, dir_fileno: Optional[int],
data: bytes, fsync: bool=True):
length_to_write = len(data)
written = 0
while written < length_to_write:
written = file_fd.write(data[written:])
if fsync:
fsync_file_and_dir(file_fd.fileno(), dir_fileno)
示例2: redirect_stream
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import fileno [as 别名]
def redirect_stream(stream_name: str, target_stream: io.FileIO):
"""Redirect a system stream to the specified file.
If ``target_stream`` is None - redirect to devnull.
"""
if target_stream is None:
target_fd = os.open(os.devnull, os.O_RDWR)
else:
target_fd = target_stream.fileno()
system_stream = getattr(sys, stream_name)
os.dup2(target_fd, system_stream.fileno())
setattr(sys, '__{}__'.format(stream_name), system_stream)
示例3: streamer
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import fileno [as 别名]
def streamer():
# Start by loading up available options
args, other_args = util.parseopts()
debug('Setting up command channel')
pid = os.getpid()
with open(args.pidfile, 'w') as pidfile:
pidfile.write('{0}'.format(pid))
cmd_fifo = '/tmp/rscad_streamer_{0}'.format(pid)
if os.path.exists(cmd_fifo):
# This shouldn't happen try to rm it
os.unlink(cmd_fifo)
os.mkfifo(cmd_fifo)
cmd_chan = FileIO(cmd_fifo, 'r+')
# Load up plugins and parse plugin specific command line opts
debug('loading plugins')
plugin_args = loadPlugins(args.path, args.plugins, other_args)
# get an appropriate rscad object
debug('making rscad obj')
RSCAD = rscad.rscadfactory(args.rscad, args.ffile)
debug('RSCAD: %s' % (type(RSCAD)))
## Need a (e)poll object - plugins implement input
# If we're on linux, use epoll's level triggered event interface,
# it's a fast poll()!
try:
poller = select.epoll()
except AttributeError:
# Not on linux, use poll()
try:
poller = select.poll()
except:
# Don't have poll() either? Quit using windows!
print('Must be run a platform that supports poll() or epoll()')
# Add the command channel to the poller
poller.register(cmd_chan.fileno(), select.POLLIN)
# Init plugins - set up (e)poll for cases that care
[poller.register(fileno, select.POLLIN) for fileno in [
p.init(plugin_args) for p in RSCADPlugin.plugins] if
fileno is not None]
## get any plugin commands
debug('Registering plugin specific commands')
pcommands = dict()
[pcommands.update(p.register_commands()) for p in RSCADPlugin.plugins]
# Need to write rscad script to RSCAD before starting the event loop
## Hook up piping
RSCAD.connect()
## main loop
try:
debug('starting main loop')
while True:
debug('Looping...')
## read script file until EOF, pumping it to RSCAD
rscad_file = RSCAD.makefile()
while True:
line = args.script.readline()
if line == '':
# rewind the file for the next pass
try:
args.script.seek(0, 0)
except IOError:
# probably stdin
pass
break
rscad_file.write(line)
rscad_file.flush()
## Wait for sequence point
for line in RSCAD.waitforsync('seq1'):
debug('loop line: %s' % (line))
[p.handle_output(line) for p in RSCADPlugin.plugins]
# check for incomming data
fd = poller.poll(0)
debug('Got filedes {0}'.format(fd))
if cmd_chan.fileno() in [fdes[0] for fdes in fd]:
handle_command(cmd_chan, pcommands)
else:
# loop through plugins calling handle_data
# it's up to the plugin to make sure the data belongs to it
[[p.handle_input(filedes[0], RSCAD) for p in
RSCADPlugin.plugins] for filedes in fd]
time.sleep(args.sleeptime)
finally:
debug('Cleaning up')
#.........这里部分代码省略.........
示例4: streamer
# 需要导入模块: from io import FileIO [as 别名]
# 或者: from io.FileIO import fileno [as 别名]
def streamer():
# Start by loading up available options
args, other_args = util.parseopts()
## Need the main loop right away
main_loop = pyev.default_loop(debug=args.debug)
try:
debug('Setting up command channel')
pid = os.getpid()
try:
with open(args.pidfile, 'w') as pidfile:
pidfile.write('{0}'.format(pid))
except IOError, e:
if e.errno == 13:
print "Permission denied openeing pidfile: {0}".format(
args.pidfile)
sys.exit(e.errno)
cmd_fifo = '/tmp/rscad_streamer_{0}'.format(pid)
if os.path.exists(cmd_fifo):
# This shouldn't happen try to rm it
os.unlink(cmd_fifo)
os.mkfifo(cmd_fifo)
cmd_chan = FileIO(cmd_fifo, 'r+')
# Load up plugins and parse plugin specific command line opts
debug('loading plugins')
plugin_args = loadPlugins(args.path, args.plugins, other_args)
# need these, even if empty
hooks = {
'plugin_commands': dict(),
'cleanup' : list(),
'input' : list(),
'output' : list(),
'filters' : list(),
}
# Add the command channel to the poller
w = pyev.Io(cmd_chan.fileno(), pyev.EV_READ, main_loop, handle_command,
data=[cmd_chan, hooks['plugin_commands']])
w.start()
for p in RSCADPlugin.plugins:
r = p.init(plugin_args)
# Extract hooks
if r.has_key('input'):
hooks['input'].append(r['input'])
if r.has_key('output'):
hooks['output'].append(r['output'])
if r.has_key('commands'):
hooks['plugin_commands'].update(r['commands'])
if r.has_key('cleanup'):
hooks['cleanup'].append(r['cleanup'])
if r.has_key('filter'):
hooks['filters'].append(r['filter'])
## Setup rscad obj
debug('making rscad obj')
RSCAD = rscad.rscadfactory(args.rscad, hooks)
debug('RSCAD type: {0}'.format(type(RSCAD)))
# if debug:
w = pyev.Signal(signal.SIGINT, main_loop, ctlc)
w.start()
# else:
# #daemonize
# pass
main_loop.data = RSCAD
debug('Starting main loop')
main_loop.start()
debug('FOOOOO')