Python Epoll has function called epoll.unregister which removes a registered file descriptor from the epoll object. Does any one know what is the function in Kqueue which is similar this. For kqueue I could only find how to delete events.
You use kqueue.control
to register or unregister an event.
An example:
import select
import os
os.mkfifo('my.fifo')
f = os.open('my.fifo', os.O_RDONLY|os.O_NONBLOCK)
try:
kq = select.kqueue()
# Add FD to the queue
kq.control([select.kevent(f, select.KQ_FILTER_READ, select.KQ_EV_ADD|select.KQ_EV_ENABLE)], 0)
# Should break as soon as we received something.
i = 0
while True:
events = kq.control(None, 1, 1.0) # max_events, timeout
print(i, events)
i += 1
if len(events) >= 1:
print('We got:', os.read(f, events[0].data))
break
# Remove FD from the queue.
kq.control([select.kevent(f, select.KQ_FILTER_READ, select.KQ_EV_DELETE)], 0)
# Should never receive anything now even if we write to the pipe.
i = 0
while True:
events = kq.control(None, 1, 1.0) # max_events, timeout
print(i, events)
i += 1
if len(events) >= 1:
print('We got:', os.read(f, events[0].data))
break
finally:
os.close(f)
os.remove('my.fifo')
You could also check the test case for kqueue to see how it's used. (And like select()
, the file descriptor could be any Python objects with a fileno()
method as well.)