[英]Akka.Net file IO - process only the first n bytes of a file
I have this very simple stream to process a file - just a FileIO.FromFile
source, a pass-through flow, and a Last
sink: 我有一个非常简单的流来处理文件-只是
FileIO.FromFile
源,传递流和Last
器:
var source = FileIO.FromFile(new FileInfo(fileName));
var flow = Flow.FromFunction<ByteString, ByteString>(x =>
{
Log.Info(x.Count.ToString());
return x;
});
var sink = Sink.Last<ByteString>();
var runnable = source.Via(flow).ToMaterialized(sink, Keep.Right);
var result = runnable.Run(Context.Materializer()).Result;
The stream runs as expected: the logger spams out the bytestring sizes until the file source is completely exhausted. 流按预期方式运行:记录器将按字节大小发送垃圾邮件,直到文件源完全耗尽。
Now, I change the sink to use First instead of Last 现在,我将接收器更改为使用First而不是Last
var source = FileIO.FromFile(new FileInfo(fileName));
var flow = Flow.FromFunction<ByteString, ByteString>(x =>
{
Log.Info(x.Count.ToString());
return x;
});
var sink = Sink.First<ByteString>();
var runnable = source.Via(flow).ToMaterialized(sink, Keep.Right);
var result = runnable.Run(Context.Materializer()).Result;
The documentation for "First" states "cancels after receiving one element", which I assumed meant the sink signals a cancel up the stream, which would close the source. “第一”文档说明“接收到一个元素后取消”,我认为这意味着接收器发出取消流的信号,这将关闭源。 But when this stream is run, two things happen.
但是,当运行此流时,会发生两件事。
1) I get the following debug log message 1)我收到以下调试日志消息
[DEBUG][17/05/2018 13:55:16][Thread 0004][akka://Demo/user/DATReader/StreamSupervisor-0/Flow-0-1-fileSource] Unhandled message from akka://Demo/user/DATReader/StreamSupervisor-0/Flow-0-0-unknown-operation : Akka.Streams.Actors.Cancel
and 和
2) The file is locked, so any further attempt to read it fails with an access denied exception. 2)文件被锁定,因此任何进一步的读取尝试均失败,并显示访问被拒绝的异常。
I also tried using Take(1)
on the source, but the same effect is seen. 我也尝试在源代码上使用
Take(1)
,但是可以看到相同的效果。
My question is: how do I read only the first n bytes from a file and shut down the stream gracefully so that any lock (obtained by FileIO.FromFile) is released? 我的问题是:如何仅从文件中读取前n个字节并正常关闭流,以便释放任何锁(由FileIO.FromFile获取)?
In my case, there is no such error. 就我而言,没有这样的错误。 Try using sharedKillSwitch to control completion.
尝试使用sharedKillSwitch来控制完成。 Documentation is here about Dynamic stream handling
有关动态流处理的文档在此处
var sharedKillSwitch = KillSwitches.Shared("my-kill-switch");
var source = FileIO.FromFile(new FileInfo(fileName), chunkSize: 1024)
.Via(sharedKillSwitch.Flow<ByteString>());
var sink = Sink.First<ByteString>();
var runnable = source.Select(x => {
Log.Info(x.Count.ToString());
sharedKillSwitch.Shutdown();
return x;
})
.ToMaterialized(sink, Keep.Right);
var result = runnable.Run(materializer).Result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.