[英]Tornado PipeIOStream: OSError: [Errno 9] Bad file descriptor
I've a little micro webservice which stores messages locally identified by IDs. 我有一个微型微服务,用于存储由ID本地标识的消息。 To ensure that files won't be written at the same time, I've implemented a queue.
为了确保不会同时写入文件,我实现了一个队列。 The following code works just once, a second file upload throws traceback below, I really don't know how to handle the fd correctly.
以下代码仅工作一次,第二个文件上传将在下面引发回溯,我真的不知道如何正确处理fd。
from tornado import web, ioloop, gen
from tornado.queues import Queue
from tornado.iostream import PipeIOStream
class Sample:
def __init__(self):
self.queue = Queue()
@gen.coroutine
def write_queue(self):
while True:
item = yield self.queue.get()
print("Message with id %s stored" % item[0])
fd = open(item[0], 'ab')
stream = PipeIOStream(fd.fileno())
yield stream.write(item[1])
stream.close_fd()
class MainHandler(web.RequestHandler):
def initialize(self, store):
self.store = store
@gen.coroutine
def put(self, id):
yield self.store.queue.put((id, self.request.body))
def start(store):
return web.Application([
(r"/(.*)", MainHandler,
{"store": store})
])
if __name__ == '__main__':
store = Store()
app = start(store)
app.listen(8888)
ioloop.IOLoop.current().add_callback(store.write_queue)
ioloop.IOLoop.current().start()
ERROR:tornado.application:Exception in callback functools.partial(<function wrap.<locals>.null_wrapper at 0x7f46657f46a8>, <Future finished exception=OSError(9, 'Bad file descriptor')>)
Traceback (most recent call last):
stream = PipeIOStream(fd.fileno())
File "/usr/local/lib/python3.5/dist-packages/tornado/iostream.py", line 1643, in __init__
self._fio = io.FileIO(self.fd, "r+")
OSError: [Errno 9] Bad file descriptor
The file descriptor returned by open
is not a pipe. open
返回的文件描述符不是管道。 It's generally legal to use regular files with PipeIOStream
, but it's not useful on linux. 通常,将常规文件与
PipeIOStream
一起使用是合法的,但是在Linux上却没有用 。 Such file descriptors are always considered readable, and reading from them or writing to them always blocks. 此类文件描述符始终被认为是可读的,并且从它们读取或写入它们始终是块。 So using PipeIOStream like this is no better than simply doing
fd.write(item[1])
. 因此,像这样使用PipeIOStream并不比简单地执行
fd.write(item[1])
。
You've opened the file in write-only mode, but PipeIOStream wraps its file in a read/write wrapper (I'm actually kind of surprised that this ever works, since real pipes are one-way). 您已经以只写模式打开了文件,但是PipeIOStream将其文件包装在读/写包装器中(我确实感到惊讶,因为实际管道是单向的,所以它曾经工作过)。 I think that's where this exception is coming from.
我认为这就是异常的来源。 If you opened the file in
'ab+'
mode, I think it would work. 如果您以
'ab+'
模式打开文件,我认为它可以工作。 I haven't tried this, though. 我还没有尝试过。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.