status-im / nim-chronos

Chronos - An efficient library for asynchronous programming
https://status-im.github.io/nim-chronos/docs/chronos
Apache License 2.0
352 stars 51 forks source link

Reading a file asynchronously? #501

Open jgirvin-venturi opened 4 months ago

jgirvin-venturi commented 4 months ago

We've been porting our threadpool based TCP and UDP servers over to chronos instead, which has been quite lovely for the most part.

One thing I've hit though, is the inability to read a file from the filesystem in an async manner?

I've attempted to use std/asyncfile with chronos instead of asyncdispatch, but unless I'm doing something obviously wrong, it seems to have an issue with the Future type from asyncfile.readAll not matching what the chronos await expects.

Please excuse the gross raises.

proc loadFirmware(device: DeviceDetails, version: string): Future[(string, int, FirmwareState)] {.async: (raises: [Exception]).}=
  # TODO: Add in parsing of board ID to check for firmware blob
  let fwPath = getCurrentDir() / "firmware" / $device.boardFamily / $device.boardRevision / version & ".bin"
  log.debug("Firmware loaded path: " & fwPath, device.boardId)
  var fwStream = openAsync(fwPath, fmRead)

  if isNil(fwStream):
    raise newException(IOError, "Cannot read binary firmware file, please check it exists...")

  log.info("Reading firmware data into memory...")
  var fwDataLen = getFileSize(fwPath).int
  var data = await fwStream.readAll()

  fwStream.close()

  return (data, fwDataLen, fwRead)

This gives the following error:

type mismatch: got <Future[system.string]>

but expected one of:
template await[T, E](fut: InternalRaisesFuture[T, E]): T
first type mismatch at position: 1
required type for fut: InternalRaisesFuture[await.T, await.E]
but expression 'readAll(fwStream)' is of type: Future[system.string]
template await[T](f: Future[T]): T
first type mismatch at position: 1
required type for f: Future[await.T]
but expression 'readAll(fwStream)' is of type: Future[system.string]
expression: await readAll(fwStream)

Any ideas? Am I missing something obvious within chronos?

arnetheduck commented 4 months ago

This is currently not supported by chronos natively - open option is to copy asyncfile to your project and port it to chronos.