task.read()
Starts a background path read.
Signature
task.read(path[, mode_or_limit])
Parameters
Text path, optional r/rb mode or nonnegative signed byte limit as for core read.
Returns
Task returning text or packed bytes.
Behavior
The worker opens and closes the file.
Errors
Denied permission or invalid arguments. An I/O/read error appears during wait.
Example
use task
use fs
p = fs.temporary_file()
try:
write(p, 'Atom')
assert(task.wait(task.read(p)) == 'Atom')
finally:
fs.remove(p)
Read text on a background task
Start the read, then wait for the result when you need it.
use fs
use task
path = fs.temporary_file()
try:
write(path, 'Atom')
print(task.wait(task.read(path)))
finally:
fs.remove(path)
Atom
The default mode returns text.
Read bytes on a background task
Use binary mode when the file should be treated as bytes.
use binary
use fs
use task
path = fs.temporary_file()
try:
write(path, 'A')
print(binary.hex(task.wait(task.read(path, 'rb'))))
finally:
fs.remove(path)
41
The read returns a byte span. Here its ASCII byte is displayed in hexadecimal.