# Waiting on a file: poll\_fd and RawFD

**URL:** <https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876>\
**Category:** General Usage\
**Created:** [February 3, 2017, 5:10pm UTC](https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876 "2017-02-03T17:10:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [February 3, 2017, 5:10pm UTC](https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876/1 "2017-02-03T17:10:08Z")

</div>

Hello,  
I’ve found a few different ways to wait on a file until data is available to read. Of the methods I’ve found, `poll_fd` seems to match my use case well, but I worry that it requires the use of `RawFD`, which only [appears in the docs once](http://docs.julialang.org/en/latest/search.html?q=RawFD) so I’m not sure I’m using it right or should be using it. `f=open("test","w");poll_fd(RawFD(fd(f)),1;readable=true)` seems to work (0.5.0 on MacOS), is there anything wrong with that usage?

This seems like a good topic for an issue/pr related to improved docs, agreed?

Thanks

PS: For what it’s worth, here are the other methods I found:

- `watch_file("test", 1)`
- `f=open("test","w");wait(RawFD(fd(f));readable=true)`
- `f=open("test","w");read(f,10;all=true)`
  - the [docs](http://docs.julialang.org/en/latest/stdlib/io-network.html#Base.read) says it “If all is true (the default), this function will block repeatedly trying to read all requested bytes, until an error or end-of-file occurs. Note that not all stream types support the all option.” This call returns immediately with an empty array, so I guess files on MacOS don’t support the all option?

---

<div class="post-metadata">

**Author:** ![ggggggggg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ggggggggg/32/265_2.png) [@ggggggggg](https://discourse.julialang.org/u/ggggggggg)\
**Post date:** [February 3, 2017, 5:59pm UTC](https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876/2 "2017-02-03T17:59:37Z")

</div>

Well it seems maybe I am doing something wrong, the following code reliable gives a bunch of libuv errors then kills julia on my machine:

```julia
names = [tempname() for i=1:20]
fs = [open(name,"w") for name in names]
fs2 = [open(name,"r") for name in names]
f=fs[1]

function write_a_lot(f)
  while true
    sleep(2)
    write(f,UInt8(0))
    flush(f)
  end
end
writers = [@schedule write_a_lot(f) for f in fs]

function wait_a_lot(f)
  while true
    read(f)
    poll_fd(RawFD(fd(f)),0.08;readable=true)
  end
end
readers = [@schedule wait_a_lot(f) for f in fs2]

```

[gist with code and output](https://gist.github.com/ggggggggg/f858778e202be0bc4f8a3382348b2549)

Any ideas? Otherwise I’ll just use `watch_file` for now, which seems to work, but seems like it should be slightly less efficient since it parses the filename every call and creates new file descriptors.

---

<div class="post-metadata">

**Author:** ![jameson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jameson/32/23_2.png) [@jameson](https://discourse.julialang.org/u/jameson)\
**Post date:** [February 5, 2017, 5:49am UTC](https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876/3 "2017-02-05T05:49:48Z")

</div>

It sounds like you are actually looking for `Pipe()` (and the not-terribly obvious `Base.link_pipe` function for opening them)? The linux design of `(e)poll` is rather rough. Julia tries to work around some of the issues with the kernel interface, but file descriptors pointing to files aren’t entirely valid as targets of `poll_fd`, just as a fair warning.

> This call returns immediately with an empty array

That’s exactly what it’s documented to do: read the whole file and return the entire contents. If you later change the file (e.g. call write), that’s a different issue, as a filesystem is not capable of performing the operation you are describing.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 6, 2017, 8:55pm UTC](https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876/4 "2017-02-06T20:55:45Z")

</div>

x-ref: [https://github.com/JuliaLang/julia/pull/20460](https://github.com/JuliaLang/julia/pull/20460)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 7, 2017, 1:43am UTC](https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876/5 "2017-02-07T01:43:12Z")

</div>

> [@ggggggggg](#):
>
> I’ve found a few different ways to wait on a file until data is available to read.

The normal thing is just to call a `read` function in a task (a “green thread”, e.g. spawned with `@async`). The task will wake up when there is data available to read (assuming another task is also blocked or yielding).

---

<div class="post-metadata">

**Author:** ![HanD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hand/32/213908_2.png) [@HanD](https://discourse.julialang.org/u/HanD)\
**Post date:** [September 18, 2020, 7:35am UTC](https://discourse.julialang.org/t/waiting-on-a-file-poll-fd-and-rawfd/1876/6 "2020-09-18T07:35:08Z")

</div>

I just tried that, but it doesn’t seem to work. A read operation on a stream that has no data available blocks the entire process, the async task does not yield to other coroutines.
