[英]How can tornado use pipe to async send data to client?
對於龍卷風,我使用多重處理來創建子進程來執行壓縮工作。 子進程通過os.pipe
直接將數據發送到主進程
正確,現在的問題是self.stream.read_until_close
阻止了該調用,並且tar_worker
沒有關閉管道的另一側。
我的代碼清單如下:
def tar_worker(fd, path):
'''Tar worker to do the compressing work, directly write data to pipe.'''
fp = os.fdopen(fd, 'w')
tar = tarfile.open(mode='w|', fileobj=fp, dereference=True)
tar.add(path)
tar.close()
fp.close()
class getTarFileHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@gen.coroutine
def get(self):
recv, sender = os.pipe()
self.p = multiprocessing.Process(target=tar_worker, args=(sender, '/boot/'))
self.p.start()
self.fd = recv
# Create PipeIOStream
self.stream = tornado.iostream.PipeIOStream(self.fd)
self.stream.read_until_close(callback=self.f, streaming_callback=self.send_data)
def f(self, s):
print 'done send work'
self.finish() #close connection
def send_data(self, chunk):
self.write(chunk)
self.flush()
文件句柄可以在多個進程之間共享; 在這種情況下,父進程和子進程都具有指向同一基礎管道兩端的文件描述符。 父進程和子進程都必須關閉sender
才能真正關閉。 啟動multiprocessing.Process
,父進程必須執行os.close(sender)
以關閉其管道寫入端的副本。 這不會阻止子進程寫入其副本; 一旦父母雙方都關閉了管道寫入端的副本,管道的讀取端將報告EOF,而IOStream讀取方法將引發StreamClosedError。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.