处理时出现异常生成器.send()在try…除了b

2024-06-09 18:39:03 发布

您现在位置:Python中文网/ 问答频道 /正文

filename = 'tempfile'

def tail(filename):
    fd = open(filename)
    while True:
        line = fd.readline()
        if not line:
            continue
        else:
            if filename != 'uh':
                yield line
            else:
                print 'Returning f to close the file'
                yield fd


try:
    genObj = tail(filename) 
    valfromgen= genObj.next()
    while valfromgen:
        print valfromgen
        valfromgen= genObj.next()
except:
    traceback.print_exc()
    try:
        fd_Got_Back = genObj.send('uh')
        fd_Got_Back.close()
    except:
        traceback.print_exc()

代码的意图:我只在generator函数中打开了文件,而不是在它外部打开的,但是,我想通过使用'send'关闭生成器函数之外的文件。在

我要做的是:从unix复制tail -f。在

我是如何做到的:

  1. 以读取模式打开临时文件。在
  2. 如果tempfile中有一个新行被写入(我将继续手动写入并使用记事本保存tempfile),则生成新写入的行。在

问题:

问题是,当python代码在命令提示符下运行时,如果我按Ctrl+C(即SIGTERM),如何从python代码中关闭打开的temp文件。为了模拟这一点,我在tail函数中打开了tempfile,每当出现异常(当我按下Ctrl+C时,系统将引发该异常),控件应该转到1st中,除非。然后,从这里开始,我尝试向生成器函数uh发送一个值tail,这样它就可以生成打开的文件的文件描述符,我可以用它来关闭打开的tempfile。在

PS:我希望有一个解决方案,我只在generator函数中打开文件,而不是在它外部打开文件。在


Tags: 文件函数代码iflinefilenametempfileelse
2条回答

我已经解决了我被困的问题,我想出了这个办法解决方案:在

  1. 当我按下Ctrl+C(在Windows上)时,键盘中断实际上发生在fd.readline公司(). 所以,我只放置了一个try…除了这里,这样每当按下Ctrl+C时,生成器函数都会生成文件描述符。如果没有键盘中断,那么,只需从tempfile打印一个新读取的行
  2. 在主体中使用isinstance()检查这个文件描述符,如果发现它是一个文件,那么我将关闭该文件和生成器

PS:(此键盘中断在Linux上可能有所不同..可能会引发SigTerm,但是,请检查。因此,为了使代码通用,只需删除键盘中断并使用normal except)

import sys,  traceback

filename = 'tempfile'

def tail(filename):
    fd = open(filename)
    while True:
        try:
            line = fd.readline()
        except KeyboardInterrupt:
            print 'keyboard interrupt here'
            yield fd
        if not line:
            continue
        else:
            yield line


try:
    genObj = tail(filename) 
    valfromgen= genObj.next()
    while valfromgen:       
        if isinstance(valfromgen, file):
            print 'Closing this file now as `tail` yielded a file descriptor'
            valfromgen.close()
            genObj.close()
            break
        print 'Yielded line: ', valfromgen
        valfromgen= genObj.next()

    print 'Just in order to check that things are in order, the following line will raise StopIteration. If it raises, it means we are good.'
    print genObj.next()
except:
    traceback.print_exc()

我想你误解了“发送”的工作原理。Send只会导致生成器在其下一次迭代中生成该值。它不会更改原始参数的值。然后,您可以将产生的价值用于某些目的。所以你可以让你的代码:

filename = 'tempfile'

def tail(filename):
    fd = open(filename)
    while True:
        line = fd.readline()
        if not line:
            continue
        else:
            x = (yield line)
            if (x == 'uh'):
                print 'Returning f to close the file'
                yield fd


try:
    genObj = tail(filename) 
    valfromgen= genObj.next()
    while valfromgen:
        print valfromgen
        valfromgen= genObj.next()
except:
    traceback.print_exc()
    try:
        genObj.send('uh').close()
    except:
        traceback.print_exc()

相关问题 更多 >