Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading a file asynchronously? #501

Open
jgirvin-venturi opened this issue Feb 13, 2024 · 1 comment
Open

Reading a file asynchronously? #501

jgirvin-venturi opened this issue Feb 13, 2024 · 1 comment

Comments

@jgirvin-venturi
Copy link

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
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants