在Caml Async中等待Writer.write完成 [英] Waiting for Writer.write to complete in Caml Async

查看:89
本文介绍了在Caml Async中等待Writer.write完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的OCaml异步作业,该作业应该写入文件并终止该过程.

I have the following simple OCaml Async job which is supposed to write to a file and terminate the process.

Unix.openfile "foobar" ~mode:[`Creat;`Rdwr]
>>= fun fd ->
let wr = Writer.create fd in
Writer.write wr "this is a test";
Unix.close fd
>>= fun () ->
exit 0

但是,似乎fd在执行写操作之前已关闭(显示的sexp的一部分是"writer fd意外关闭").有没有办法在关闭文件描述符之前等待写入完成?实际上,我不理解为什么Writer.write不像Reader.read那样返回Deferred.t.它不能解决问题吗?

However, it seems that fd gets closed before the write is performed (part of displayed sexp is "writer fd unexpectedly closed"). Is there a way to wait for write to complete before closing the file descriptor? Actually, I don't understand why Writer.write doesn't return a Deferred.t just as Reader.read does. Wouldn't it solve the issue?

我的问题实际上更笼统.基本上,我有一个定期的工作,该工作使用Clock.every'向文件中写入内容.该程序可以随时退出并关闭文件描述符.如何确保在fd关闭之前处理所有写操作?

My problem is actually a little more general. Basically, I have a periodic job that writes something to a file using Clock.every'. The program can exit at any time and close the file descriptors. How to make sure that all the writes are processed before the fd get closed?

如果我的停止工作是:

Unix.close fd
>>= fun () ->
exit 0

Unix.close fdexit 0之间很可能会发生计划的写入.

A scheduled write can very well happen between Unix.close fd and exit 0.

推荐答案

在您的特定情况下,您不应直接使用Unix.close函数关闭文件描述符.这个想法是,您实际上将fd的所有权移给了编写者,因此关闭文件描述符现在是编写者的责任.因此,您需要使用Writer.close返回延迟的单位.此函数将等待所有未完成的写操作完成,然后关闭文件描述符,例如

In your particular case, you shouldn't close the file descriptor directly with the Unix.close function. The idea is that you actually moved the ownership of fd to the writer, so it is now the writer's responsibility to close the file descriptor. So, you need to use Writer.close that returns a unit deferred. This function, will wait until all pending writes are finished, and then close the file descriptor, e.g.,

Unix.openfile "foobar" ~mode:[`Creat;`Rdwr]
>>= fun fd ->
let wr = Writer.create fd in
Writer.write wr "this is a test";
Writer.close fd
>>= fun () ->
exit 0

回答一个更一般的问题:在关闭文件描述符之前,是否有一种等待写入完成的方法?".是的,Writer.close将等待.从某种意义上说,它将返回一个延迟,该延迟将在写入所有内容并关闭fd之后确定.您还可以使用force_close参数,如果该参数被认为是挂起程序的更好选择,则该参数将强制执行关闭操作导致突然写入.例如,您可以给程序一个合理的时间来刷新数据,然后以错误终止.

Answering your more general question "Is there a way to wait for write to complete before closing the file descriptor?". Yes, Writer.close will wait. In the sence that it will return a deferred that will become determined after everything is written and fd is closed. You can also use a force_close parameter, that will force the closing operation leading to abrupt writes, if it considered a better option to the hanging program. E.g., you can give a program a reasonable amount of time to flush data, and then terminate with an error.

这篇关于在Caml Async中等待Writer.write完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆