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

monitoring arbitrary kevents #579

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions trio/_core/_io_kqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,32 @@ def current_kqueue(self):

@_public
@contextmanager
def monitor_kevent(self, ident, filter):
def monitor_kevent(self, ident, filter, fflags=0):
key = (ident, filter)
if key in self._registered:
raise _core.ResourceBusyError(
"attempt to register multiple listeners for same "
"ident/filter pair"
)

flags = select.KQ_EV_ADD
event = select.kevent(ident, filter, flags, fflags)
self._kqueue.control([event], 0)

q = _core.UnboundedQueue()
self._registered[key] = q
try:
yield q
finally:
del self._registered[key]
try:
del self._registered[key]
except KeyError:
# was a one shot event and already got deleted?
pass
else:
flags = select.KQ_EV_DELETE
event = select.kevent(ident, filter, flags)
self._kqueue.control([event], 0)

@_public
async def wait_kevent(self, ident, filter, abort_func):
Expand Down
25 changes: 25 additions & 0 deletions trio/_core/tests/test_kqueue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import select
import subprocess

import pytest

from ... import _core

use_kqueue = hasattr(select, 'kqueue')
pytestmark = pytest.mark.skipif(not use_kqueue, reason="kqueue platforms only")


async def test_kqueue_monitor_proc_event():
p = subprocess.Popen(['sleep', '0.1'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So all that stuff about zombies means you don't need this process to sleep. Maybe true and false and then you can also check the exit code?

ident = p.pid
filter = select.KQ_FILTER_PROC
fflags = select.KQ_NOTE_EXIT
with _core.monitor_kevent(ident, filter, fflags) as q:
async for batch in q:
for evt in batch:
assert evt.ident == ident
assert evt.filter == filter
assert evt.fflags == fflags
assert evt.data == 0 # exit code
# we only expect one event for KQ_NOTE_EXIT
return