nim-chronos icon indicating copy to clipboard operation
nim-chronos copied to clipboard

Reading a file asynchronously?

Open jgirvin-venturi opened this issue 4 months ago • 1 comments

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?

jgirvin-venturi avatar Feb 13 '24 06:02 jgirvin-venturi